Skip to content

Instantly share code, notes, and snippets.

@luismasuelli
Created January 18, 2021 17:03
Show Gist options
  • Save luismasuelli/a0b850edd994820b9d9fb1af3440328b to your computer and use it in GitHub Desktop.
Save luismasuelli/a0b850edd994820b9d9fb1af3440328b to your computer and use it in GitHub Desktop.
Advanced name validator
# This code can easily be adapted to Python 3.
from __future__ import unicode_literals
from django.core.validators import RegexValidator as _RegexValidator
import unicodedata
from django.utils import six
# Another validator that could be useful: A regex validator using ^[\w_-]$.
class AccentsAgnosticRegexValidator(_RegexValidator):
"""
This validator performs a validation after normalizing the unicode
string. This means: will validate as if the string value did not
have accents in letters.
"""
def __call__(self, value):
return super(AccentsAgnosticRegexValidator, self).__call__(
''.join(c for c in unicodedata.normalize('NFD', six.text_type(value)) if unicodedata.category(c) != 'Mn')
)
class NameRegexValidator(AccentsAgnosticRegexValidator):
"""
This validator performs a validaron on written names / words, on any
kind of alphabet which can be reduced to latin letters. Perhaps
strange characters (e.g. greek, cyrillic) can be "normalized" to
latin letters, and so such characters will count as valid instead
of bust.
"""
MODE_ONLY_LETTERS = 0
MODE_ACCEPT_NUMBERS = 1
MODE_ALPHANUMERIC = 2
MODE_ALPHANUMERIC_EXTENDED = 3
def __init__(self, mode=MODE_ONLY_LETTERS, *args, **kwargs):
try:
super(NameRegexValidator, self).__init__([
r'^\s*[A-Za-z]+(\'[A-Za-z]+)?(\s+[A-Za-z]+(\'[A-Za-z]+)?)*\s*$',
r'^\s*([A-Za-z]+(\'[A-Za-z]+)?|\d+)(\s+([A-Za-z]+(\'[A-Za-z]+)?|\d+))*\s*$',
r'^\s*[A-Za-z0-9]+(\'[A-Za-z0-9]+)?(\s+[A-Za-z0-9]+(\'[A-Za-z0-9]+)?)*\s*$',
r'^\s*[A-Za-z0-9-]+(\'[A-Za-z0-9-]+)?(\s+[A-Za-z0-9-]+(\'[A-Za-z0-9-]+)?)*\s*$'
][mode], *args, **kwargs)
except IndexError:
raise ValueError('An invalid mode value (i.e. not in the 0-3 range) was assigned to NameRegexValidator')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment