2
I’m doing a regular expression to validate an alphanumeric field of up to five digits. The first field should be numerical only, the rest can be letters or numbers:
ex: 1A, 11A, 111A, 1111A, 1, 11, 111, 1111, 11111
Restrictions:
- There can’t be a letter between two numbers:
ex = 1A11, 2A2, 222A1
; - You can’t start with a letter:
ex = A11
; - There can be no letter followed by letter:
ex = AA, AA1, A1A
;
What I did first: \d?\w?\w?\w\w
It only accepts from the second character whether it is letter or number. This should not happen, because the first should be exclusively number. And accepts letters between numbers.
Then I came to it: ^(\d{0,5})([A-Z]{0,5})$
Here the only problem is that it accepts to start with letter and put letter followed by letter.