diff --git a/django/forms/fields.py b/django/forms/fields.py index ab3fe67aeb..3ff2819571 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -533,7 +533,7 @@ class ImageField(FileField): url_re = re.compile( r'^https?://' # http:// or https:// - r'(?:(?:[A-Z0-9-]+\.)+[A-Z]{2,6}|' #domain... + r'(?:(?:[A-Z0-9]+(?:-*[A-Z0-9]+)*\.)+[A-Z]{2,6}|' #domain... r'localhost|' #localhost... r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip r'(?::\d+)?' # optional port diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py index bec07dd5c3..f66f2e1bb5 100644 --- a/tests/regressiontests/forms/fields.py +++ b/tests/regressiontests/forms/fields.py @@ -900,6 +900,10 @@ u'http://example.com/' u'http://www.example.com/' >>> f.clean('http://www.example.com:8000/test') u'http://www.example.com:8000/test' +>>> f.clean('valid-with-hyphens.com') +u'http://valid-with-hyphens.com/' +>>> f.clean('subdomain.domain.com') +u'http://subdomain.domain.com/' >>> f.clean('http://200.8.9.10') u'http://200.8.9.10/' >>> f.clean('http://200.8.9.10:8000/test') @@ -924,6 +928,24 @@ ValidationError: [u'Enter a valid URL.'] Traceback (most recent call last): ... ValidationError: [u'Enter a valid URL.'] +>>> f.clean('http://invalid-.com') +Traceback (most recent call last): +... +ValidationError: [u'Enter a valid URL.'] +>>> f.clean('http://-invalid.com') +Traceback (most recent call last): +... +ValidationError: [u'Enter a valid URL.'] +>>> f.clean('http://inv-.alid-.com') +Traceback (most recent call last): +... +ValidationError: [u'Enter a valid URL.'] +>>> f.clean('http://inv-.-alid.com') +Traceback (most recent call last): +... +ValidationError: [u'Enter a valid URL.'] +>>> f.clean('http://valid-----hyphens.com') +u'http://valid-----hyphens.com/' >>> f = URLField(required=False) >>> f.clean('')