Validate email local component

I'm writing a registration form that only needs to accept the local component of a desired email address. The domain component is fixed to the site. I am attempting to validate it by selectively copying from validators.validate_email which Django provides for EmailField :

email_re = re.compile(
    r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*"  # dot-atom
    # quoted-string, see also http://tools.ietf.org/html/rfc2822#section-3.2.5
    r'|^"([01-10131416-37!#-[]-177]|[01-11131416-177])*"'
    r')@((?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?.)+[A-Z]{2,6}.?$)'  # domain
    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)  # literal form, ipv4 address (SMTP 4.1.3)
validate_email = EmailValidator(email_re, _(u'Enter a valid e-mail address.'), 'invalid')

Following is my code. My main issue is that I'm unable to adapt the regex. At this point I'm only testing it in a regex tester at http://www.pythonregex.com/ however it's failing:

^([-!#$%&'*+/=?^_`{}|~0-9A-Z]+(.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*)$

This seems to be passing undesirable characters such as ?

The entire code for my Field, which is not necessarily relevant at this stage but I wouldn't mind some comment on it would be:

class LocalEmailField(CharField):    
    email_local_re = re.compile(r"^([-!#$%&'*+/=?^_`{}|~0-9A-Z]+(.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*)$", re.IGNORECASE)
    validate_email_local = RegexValidator(email_re, (u'Enter a valid e-mail username.'), 'invalid')
    default_validators = [validate_email_local]

EDIT: To clarify, the user is only entering the text BEFORE the @ , hence why I have no need to validate the @domain.com in the validator.

EDIT 2: So the form field and label will look like this:

Desired Email Address: [---type-able area---] @domain.com


You say "undesirable characters such as ? ", but I think you're mistaken about what characters are desirable. The original regex allows question marks.

Note that you can also define your own validator that doesn't use a massive regex, and have some chance of decoding the logic later.


Some people, when confronted with a problem, think, “I know, I'll use regular expressions.” Now they have two problems. - Jamie Zawinski

Checking via regex is an exercise in wasting your time. The best way is to attempt delivery; this way not only can you verify the email address, but also if the mailbox is actually active and can receive emails.

Otherwise you'll end up in an every-expanding regular expression that can't possibly hope to match all the rules.

"Haha boo hoo woo woo!"@foo.com is a valid address, so is qwerterukeriouo@gmail.com

Instead, offer the almost-standard "Please click on the link in the email we sent to blahblah@goo.com to verify your address." approach.

If you want to create email addresses, then you can write your own rules on what can be a part of the email component; and they can be a subset of the official allowed chars in the RFC.

For example, a conservative rule (that doesn't use regular expressions):

allowed_chars = [string.digits+string.letters+'-']

if len([x in user_input if x not in allowed_chars]):
   print 'Sorry, invalid characters'
else:
   if user_input[0] in string.digits+'-':
      print 'Cannot start with a number or `-`'
   else:
      if check_if_already_exists(user_input):
         print 'Sorry, already taken'
      else:
         print 'Congratulations!'

I'm still new to Django and Python, but why reinvent the wheel and maintain your own regex? If, apart from wanting users to enter only the local portion of their email address, you're happy with Django's built-in EmailField , you can subclass it quite easily and tweak the validation logic a bit:

DOMAIN_NAME = u'foo.com'


class LocalEmailField(models.EmailField):
    def clean(local_part):
        whole_address = '%s@%s' % (local_part, DOMAIN_NAME)
        clean_address = super(LocalEmailField, self).clean(whole_address)
        # Can do more checking here if necessary
        clean_local, at_sign, clean_domain = clean_address.rpartition('@')
        return clean_local

Have you looked at the documentation for Form and Field Validation and the .clean() method?

链接地址: http://www.djcxy.com/p/92684.html

上一篇: 在之前或之后验证会员资格?

下一篇: 验证电子邮件本地组件