views:

589

answers:

2

Hi,

I want regular expression validator for my telephone field in VB.net. Please see the requirement below:

Telephone format should be (+)xx-(0)xxxx-xxxxxx ext xxxx (Optional) example my number would appear as 44-7966-591739 Screen would be formatted to show +44-(0)7966-591739 ext

Please suggest.

Best Regards, Yuv

A: 

Try this

^(((+44\s?\d{4}|(?0\d{4})?)\s?\d{3}\s?\d{3})|((+44\s?\d{3}|(?0\d{3})?)\s?\d{3}\s?\d{4})|((+44\s?\d{2}|(?0\d{2})?)\s?\d{4}\s?\d{4}))(\s?#(\d{4}|\d{3}))?$

Peymankh
Hey Peymankh, Thanks for the regular expresion. Can you please let me know what type of telephone number would be allowed as I am not able to enter my desired format (+)xx-(0)xxxx-xxxxxx ext xxxx (Optional)
MKS
It's stuff like this that gives regular expressions a bad name! Any regex this long *requires* commenting for it to be remotely readable. That aside, I'm fairly sure this expression is invalid anyway. :/
Peter Boughton
@Peter Regex is bad my friend, and I just wanted to help!! thought it's gonna work this way forYuv.
Peymankh
A: 

For validation:
As bobince points out, you should be flexible with phone numbers because there are so many ways to enter them.

One simple yet effective way to validate the value is first strip all non-numeric values, then make sure it is at least 11 digits long, and - if you're limiting to UK numbers - then check it starts with either 0 or 44.

I can't be bothered looking up vb.net syntax, but something along the lines of this:

if Phone.replaceAll('\D','').length < 11
    // Invalid Number
endif;

(The \D is regex for anything not 0-9.)


To format a number as requested, assuming you've got a relatively fixed input that you want to display to a page, something like this might work:

replace:

(\d{2,3})\D*0?\D*(\d{4})\D*(\d{5})\D*(\d*)

with:

+$1-(0)$2-$3 ext $4

That's fairly flexible but wont accept any old phone number. It currently required an international code at the start, and I'm not quite sure on the rules of them to know if it's going to work perfectly, but it might be good enough for what you need.


An explanation of that regex, in regex comment mode (so it can be used directly as a regex if necessary):

(?x)        # enable regex comment mode (whitespace ignored, hashes start comments)

# international code:
(\d{2,3})   # matches 3 or 2 digits; captured to group 1.

# optional 0 with potental spaces dashes or parens:
\D*         # matches as many non-digits as possible, none required.
0?          # optionally match a zero
\D*         # matches as many non-digits as possible, none required.

# main part of number:
(\d{4})     # match 4 digits; captured to group 2
\D*         # matches as many non-digits as possible, none required.
(\d{5})     # match 5 digits; captured to group 3.

# optional prefix:
\D*         # matches as many non-digits as possible, none required.
(\d*)       # match as many digits as possible, none required; captured to group 4.
Peter Boughton
Hold on... the question is asking about *formatting*, not *validation*. Bah!
Peter Boughton