Reputation: 517
I need to validate two possible patterns for the input using HTML5 pattern.
OR
I tried ^([0-9]{12,12})|([0-9]{9,9}[A-Za-z]{1,1})
, ^([0-9]{12})|([0-9]{9,9}[A-Za-z])$
, and many more but the problem is if user enters an alphabet character when the total length is between 9 and 12, then it takes as a valid input. But it should be not.
Valid input is either 12 digits, or 9 digits with one char.
What have I done wrong?
Upvotes: 0
Views: 49
Reputation: 63610
You could check for 9 digits at the start of the string: (see ^
, beginning of input assertion, \d
, digit character class and the x{n}
quantifier)
^\d{9}
followed by either an alphabetical character or 3 more digits, and the end of the string: (see the non capturing group (?: ... )
, [ ... ]
, the character set, x|y
and $
, end of input assertion)
(?:[a-zA-Z]|\d{3})$
So the expression would be:
^\d{9}(?:[a-zA-Z]|\d{3})$
Upvotes: 1