Validation to allow single space for phone numbers [duplicate]
-
06-07-2019 - |
Question
This question already has an answer here:
i have a validation in my .net textbox where it will take only numbers
but when i put the the phone format like
080 234234
it will not accept because of a space
how to resolve this ?
could anyone help in regular expression ?
Current expression is this [0-9]+
i want only single space ... no two spaces should be encoraged
Solution
[0-9]+\s?[0-9]+
The question mark indicates there is zero or one of the preceding element.
OTHER TIPS
This will allow a single white space after the first three digits: [0-9]{3}\s?[0-9]{6}
This would allow white spaces (only one in a row) anywhere: ([0-9]\s?)+
something like this: ([0-9]{1}[0-9]*[\s]{0,1}[0-9])+([\s]{0,1}[0-9]+)
UPDATE: the benefit of this method is that it won't allow leading or trailing spaces, and of course enforces single spaces.
\s gives one space. Maybe something like this: [0-9]{3}\s?[0-9]{6}
This means it'll take three digitis followed by an optional whitespace character and ending with six digits.