Reputation: 37
I have a regular expression that I am attempting to use to control user input to only allow letters A-D(case insensitive) followed by a numerical value between 1-16.
Ex// A12, C5 are valid entries, but B18 would be invalid as would H2.
The regular expression I am using flags incorrect input based on the alphabetical value, but not the numerical - so in my previous example it is correctly flagging H2, but is allowing B18 to be entered.
I am fairly new to regex and so I am unsure where I've gone astray. Any help would be greatly appreciated!
My current expression is as follows:
var regex = new RegExp(/([A-Da-d]{1}[1-9]{1}|[A-Da-d]{1}[1]{1}[0-6]{1})/);
Thanks again!
Upvotes: 1
Views: 31
Reputation: 30995
You could use a regex like this with insensitive flag:
^[a-d](?:1[0-6]|[0-9])$
Upvotes: 0
Reputation: 97140
This expression will do it:
/^[A-D]([1-9]|1[0-6])$/i
var re = /^[A-D]([1-9]|1[0-6])$/i;
console.log(re.test('A12'));
console.log(re.test('C5'));
console.log(re.test('H12'));
console.log(re.test('B18'));
Upvotes: 1