Fixed #9890 -- Modified the regex validation for email addresses to match RFC822/1035. Thanks to ozgur for the report, and kratorius for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10573 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2009-04-16 14:24:27 +00:00
parent 4eaf4155f8
commit 48b459a83e
2 changed files with 28 additions and 7 deletions

View File

@ -256,8 +256,8 @@ class DecimalField(Field):
digits = len(digittuple) digits = len(digittuple)
if decimals > digits: if decimals > digits:
# We have leading zeros up to or past the decimal point. Count # We have leading zeros up to or past the decimal point. Count
# everything past the decimal point as a digit. We do not count # everything past the decimal point as a digit. We do not count
# 0 before the decimal point as a digit since that would mean # 0 before the decimal point as a digit since that would mean
# we would not allow max_digits = decimal_places. # we would not allow max_digits = decimal_places.
digits = decimals digits = decimals
whole_digits = digits - decimals whole_digits = digits - decimals
@ -283,7 +283,7 @@ DEFAULT_DATE_INPUT_FORMATS = (
) )
class DateField(Field): class DateField(Field):
widget = DateInput widget = DateInput
default_error_messages = { default_error_messages = {
'invalid': _(u'Enter a valid date.'), 'invalid': _(u'Enter a valid date.'),
} }
@ -422,7 +422,7 @@ class RegexField(CharField):
email_re = re.compile( email_re = re.compile(
r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom 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 r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"' # quoted-string
r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE) # domain r')@(?:[A-Z0-9]+(?:-*[A-Z0-9]+)*\.)+[A-Z]{2,6}$', re.IGNORECASE) # domain
class EmailField(RegexField): class EmailField(RegexField):
default_error_messages = { default_error_messages = {
@ -829,7 +829,7 @@ class FilePathField(ChoiceField):
super(FilePathField, self).__init__(choices=(), required=required, super(FilePathField, self).__init__(choices=(), required=required,
widget=widget, label=label, initial=initial, help_text=help_text, widget=widget, label=label, initial=initial, help_text=help_text,
*args, **kwargs) *args, **kwargs)
if self.required: if self.required:
self.choices = [] self.choices = []
else: else:

View File

@ -745,6 +745,27 @@ ValidationError: [u'Enter a valid e-mail address.']
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValidationError: [u'Enter a valid e-mail address.'] ValidationError: [u'Enter a valid e-mail address.']
>>> f.clean('example@invalid-.com')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid e-mail address.']
>>> f.clean('example@-invalid.com')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid e-mail address.']
>>> f.clean('example@inv-.alid-.com')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid e-mail address.']
>>> f.clean('example@inv-.-alid.com')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid e-mail address.']
>>> f.clean('example@valid-----hyphens.com')
u'example@valid-----hyphens.com'
>>> f.clean('example@valid-with-hyphens.com')
u'example@valid-with-hyphens.com'
>>> f = EmailField(required=False) >>> f = EmailField(required=False)
>>> f.clean('') >>> f.clean('')
@ -1104,7 +1125,7 @@ ValidationError: [u'Select a valid choice. 6 is not one of the available choices
# TypedChoiceField ############################################################ # TypedChoiceField ############################################################
# TypedChoiceField is just like ChoiceField, except that coerced types will # TypedChoiceField is just like ChoiceField, except that coerced types will
# be returned: # be returned:
>>> f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int) >>> f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int)
>>> f.clean('1') >>> f.clean('1')
@ -1122,7 +1143,7 @@ ValidationError: [u'Select a valid choice. 2 is not one of the available choices
# This can also cause weirdness: be careful (bool(-1) == True, remember) # This can also cause weirdness: be careful (bool(-1) == True, remember)
>>> f.coerce = bool >>> f.coerce = bool
>>> f.clean('-1') >>> f.clean('-1')
True True
# Even more weirdness: if you have a valid choice but your coercion function # Even more weirdness: if you have a valid choice but your coercion function