Created
April 22, 2013 01:48
-
-
Save Ceasar/5431931 to your computer and use it in GitHub Desktop.
Email Validator for wtforms using Django validator.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Email(object): | |
message = 'Enter a valid email address.' | |
user_regex = re.compile( | |
r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*$" # dot-atom | |
r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-\011\013\014\016-\177])*"$)', # quoted-string | |
re.IGNORECASE) | |
domain_regex = re.compile( | |
r'(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?$)' # domain | |
# literal form, ipv4 address (SMTP 4.1.3) | |
r'|^\[(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\]$', | |
re.IGNORECASE) | |
def __call__(self, form, field): | |
user_part, domain_part = field.data.rsplit('@', 1) | |
if not self.user_regex.match(user_part): | |
raise ValidationError(self.message) | |
if not self.domain_regex.match(domain_part): | |
raise ValidationError(self.message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment