0

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.

1

3 Answers 3

1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Use \D, [^\d] not.
0

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.
  • This group is then quantified: (?: ){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!

View an online regex demo!

Comments

0
 (?=[\-\/]*[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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.