SmartestVEGA
SmartestVEGA

Reputation: 8889

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

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

Upvotes: 0

Views: 7101

Answers (3)

Dave Sherohman
Dave Sherohman

Reputation: 46187

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

Upvotes: 1

Michał Niklas
Michał Niklas

Reputation: 54302

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

Upvotes: 1

jasondoucette
jasondoucette

Reputation: 1156

You could use

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

or

(\d+\s*)+

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

Upvotes: 1

Related Questions