Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to allow only 5 digit number or alpha character followed by 5 digit number

I want to create validation on a form field which checks that input is a valid ID.

Most of the IDs are simple 5 digit numbers, but there a few odd variations where the number could have a leading alpha character (an F or G so eg F12345) or ending in an alpha character (an A or B so eg 12345B).

I have the regexp for a 5 digit number but I don't know where to go to allow F/G at the beginning or A/B at the end

Any ideas?

like image 810
Tom Avatar asked Dec 27 '22 20:12

Tom


1 Answers

This regex should do:

/^([FG]?\d{5}|\d{5}[AB])$/

You can use .test() function of RegExp object to validate the string.

/^([FG]?\d{5}|\d{5}[AB])$/.test("F12345")
like image 166
nhahtdh Avatar answered Dec 29 '22 11:12

nhahtdh