Reputation: 101
Why my regex
accept also letters? For example:
123abc - not working (does not display error message)
test = forms.RegexField(
max_length=15,
regex=r'^[0-9\-\+_ ]',
error_message=_(u"Only digits0-9 and +, _, -"),
required=True,
label=_(u'Test'))
It should accept string consisting of 0-9 and these special chars: +, _,-," "(space)
Upvotes: 1
Views: 122
Reputation: 31474
Your regex is only testing the start of the string - in fact it is only testing the first character of the string. If [0-9\-\+_ ]
is all you want in the whole string then stick a +$
at the end:
regex=r'^[0-9\-\+_ ]+$'
This says that the whole string, start to finish, is only allowed to contain the characters inside the square brackets.
Upvotes: 4