Validation to allow space for phone numbers
i have a validation in my .net textbox where it will take only numbers
but when i put the the phone format like
08开发者_如何学Go0 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]+
Simply add space to characters range:
[0-9][0-9 ]*
You can also add start and stop indicators:
^[0-9][0-9 ]*$
EDIT: number must start with digit followed with digits or spaces (zero or more).
You could use
([0-9]+\s*)+
or
(\d+\s*)+
either of which would allow one or more groups of digits followed by optional whitespace
Really, the best way to deal with this is to remove all non-digit characters, then do whatever additional validation you may require, such as the number of digits or whether the number begins with a valid area code/country code, on what's left. That way it doesn't matter whether the number is entered as (assuming US numbers here) 987-654-3210, (987) 654-3210, 987 654 3210, 9876543210, 9 8 7-6.54321 0, or whatever else.
Concentrate on validating what's meaningful in the input (the digits) and not incidental details which really don't matter (how the digits are grouped or formatted).
精彩评论