misc: check for correct number of digits in French phone numbers (#43556)
This commit is contained in:
parent
0aed92cc55
commit
2122f9d955
|
@ -698,6 +698,11 @@ def test_wcsextrastringwidget_phone_fr():
|
|||
mock_form_submission(req, widget, {'test': '0123456789'})
|
||||
assert not widget.has_error()
|
||||
|
||||
widget = WcsExtraStringWidget('test', value='foo', required=False)
|
||||
widget.field = fakefield
|
||||
mock_form_submission(req, widget, {'test': '01 23 45 67 89'})
|
||||
assert not widget.has_error()
|
||||
|
||||
widget = WcsExtraStringWidget('test', value='foo', required=False)
|
||||
widget.field = fakefield
|
||||
mock_form_submission(req, widget, {'test': 'az'})
|
||||
|
@ -708,6 +713,11 @@ def test_wcsextrastringwidget_phone_fr():
|
|||
mock_form_submission(req, widget, {'test': '0123'})
|
||||
assert widget.has_error()
|
||||
|
||||
widget = WcsExtraStringWidget('test', value='foo', required=False)
|
||||
widget.field = fakefield
|
||||
mock_form_submission(req, widget, {'test': '01234567890123'})
|
||||
assert widget.has_error()
|
||||
|
||||
# and check it gets a special HTML input type
|
||||
assert 'type="tel"' in str(widget.render())
|
||||
|
||||
|
|
|
@ -930,7 +930,7 @@ class ValidationWidget(CompositeWidget):
|
|||
validation_methods = collections.OrderedDict([
|
||||
('digits', {'title': N_('Digits'), 'regex': '\d+'}),
|
||||
('phone', {'title': N_('Phone Number'), 'regex': r'\+?[-\(\)\d\.\s/]+', 'html_input_type': 'tel'}),
|
||||
('phone-fr', {'title': N_('Phone Number (France)'), 'regex': '0[\d\.\s]{9}', 'html_input_type': 'tel'}),
|
||||
('phone-fr', {'title': N_('Phone Number (France)'), 'function': 'validate_phone_fr', 'html_input_type': 'tel'}),
|
||||
('zipcode-fr', {'title': N_('Zip Code (France)'), 'regex': '\d{5}'}),
|
||||
('siren-fr', {'title': N_('SIREN Code (France)'), 'function': 'validate_siren'}),
|
||||
('siret-fr', {'title': N_('SIRET Code (France)'), 'function': 'validate_siret'}),
|
||||
|
|
|
@ -705,6 +705,13 @@ def is_ascii_digit(string_value):
|
|||
return string_value and all((x in '0123456789' for x in string_value))
|
||||
|
||||
|
||||
def validate_phone_fr(string_value):
|
||||
if not re.match(r'^0[\d\.\s]+$', string_value):
|
||||
# leading zero, then digits, dots, or spaces
|
||||
return False
|
||||
return len([x for x in string_value if is_ascii_digit(x)]) == 10
|
||||
|
||||
|
||||
def validate_siren(string_value):
|
||||
return validate_luhn(string_value, length=9)
|
||||
|
||||
|
|
Loading…
Reference in New Issue