views:

203

answers:

3

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]+

A: 

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).

Michał Niklas
But that would allow for an input of all spaces to pass, ie a phone number without any digits
jasondoucette
OK. I think [0-9][0-9 ]* will be better
Michał Niklas
+1  A: 

You could use

([0-9]+\s*)+

or

(\d+\s*)+

either of which would allow one or more groups of digits followed by optional whitespace

jasondoucette
+1  A: 

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).

Dave Sherohman
+1 That is the simplest and nicest solution. Just remember to be careful with the leading + used for international numbers.
ya23