A regular expression that allows only a 5-digit number or alpha character, followed by a 5-digit number

I want to create a check in a form field that checks that input is a valid identifier.

Most identifiers are simple five-digit numbers, but there are several odd variations in which the number can have a leading alpha character (for example, F or G, for example, F12345 ) or end with an alphabetic character (A or B therefore, for example, 12345B ).

I have a regex 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?

+4
source share
1 answer

This regex should do:

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

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

 /^([FG]?\d{5}|\d{5}[AB])$/.test("F12345") 
+10
source

All Articles