Reputation: 101
I have a field which the user will be entering a 9 digit numeric string.
e.g. 975367865
However some users will be entering the 9 digit numeric string with seperators such as "-" and "/".
e.g. 9753/67/865 OR 9753-67-865
I want to make sure that the user has entered a minimum of 9 numbers even if the user has added the "-" & "/" somewhere in the string.
Hope that makes sense.
Many thanks for any help.
Upvotes: 0
Views: 92
Reputation: 67968
(?=[\-\/]*[0-9][\-\/]*[0-9][\-\/]*[0-9][\-\/]*[0-9][\-\/]*[0-9][\-\/]*[0-9][\-\/]*[0-9][\-\/]*[0-9][\-\/]*[0-9]+[\-\/]*)(.*)
Though a rather long looking regex but works perfectly well for your case. Have a look at the demo.
http://regex101.com/r/uR2aE4/3
Upvotes: 0
Reputation: 338
Match with a quantifier:
/^\d(?:\D*\d){8}$/
^$
are Anchors that asserts position at the start of end of the String. This asserts the entire match, but since you're only matching, you can leave them out!\d
matches a digit.\D*
skips ahead of any non-digits. Then a digit will be matched with \d
.(?: ){8}
To assert that the later alternation is matched eight times. Parted with the first match, this asserts that 9 digits are present in the match!Upvotes: 0
Reputation: 175748
You could just remove anything that is not a number to give a nice normalized form:
var number = input.replace(/[^\d]/g, "");
var ok = number.length == 9;
Upvotes: 1