0

Where is my mistake?

mobile = forms.IntegerField(
    label='Mobile',
    widget=forms.NumberInput(attrs={
        'class': 'form-control',
        'required': 'required'
    }),
    error_messages={
        'invalid': 'Required',
    }
)

def clean_mobile(self):
    mobile = self.cleaned_data['mobile']

    if not re.match('^((?!([0-8]))[0-9]{9})$', str(mobile)):
        self.add_error('mobile', 'Wrong number')

    return mobile

Always returns False!

Chalist
  • 3,160
  • 5
  • 39
  • 68

1 Answers1

2

In your regex you are looking for a digit which is not in range [0-8] at beginning of input string that literally means 9. Negative lookahead asserts then gives back cursor at position before first character. You then look for 9 digits and end of string which immediately fails on 10 digit numbers.

Try this:

^9\d{9}$
revo
  • 47,783
  • 14
  • 74
  • 117