tags:

views:

765

answers:

8

Hi,

I want VB.NET regular expression for below format

7966-591739

I mean user must have to enter 4 numeric characters first then "-" and then 6 numeric characters after that optional for numeric as well as for characters. In my case user can also enter

7966-591739 ext 562

7966-591739 x 434

Please suggest!

A: 

Try this:

'[0-9]{4}\-[0-9]{6}( (ext|x) [0-9]{3})?'

NawaMan
Thanks. How can I have optional for 4 and 5 first digits in regular expresion.
MKS
change the `{4}` to `{4,5}`
nickf
And what about when I need optional for 4,5,6 for first numbers?
MKS
+2  A: 

I'd suggest not - some uk numbers have five digits before the -

wefwfwefwe
+7  A: 

There is no standard UK phone format

Allow the user to type in whatever they want and strip out the non numeric characters

Example phone numbers include (all from an address book)

020 7xxx xxxx
017373 xxxxx
0796 xxxx xxx
07780 xxx xxx

Also do you use parentheses or -

There is also the international form +44 and drop the 0 (often wrongly done as +44 (0)20 7xxx xxxx)

It really annoys me when I can't type my phone number into a form - even worse paste it from somewhere

Mark
Then Please suggest what should do for above problem?
MKS
There are also short numbers begining with 1 (118xxx etc) and also 999, but I would suspect those are beyond the scope of the questioner's needs.
billpg
What problem? Just allow one string field allowing more than 15 characters. Then when you dial just use the numeric characters
Mark
+1 trying to ‘validate’ phone numbers is a losing game. You stand only to annoy and lose customers whose phone numbers you thing are ‘invalid’.
bobince
A: 

I belive that UK number often have 5 digits before your '-' character.

I've updated @NawaMan's answer to make it run and to remove the capturing groups:

^[0-9]{4,6}\-[0-9]{6}(?:\s(?:ext|x)\s[0-9]{3,4})?$

And here to capture the number groups:

^([0-9]{4,6})\-([0-9]{6})(?:\s(?:ext|x)\s([0-9]{3,4}))?$

And here is the expression cleaned up:

^(\d{4,6})\-(\d{6})(?:\s(?:ext|x)\s(\d{3,4}))?$


UPDATED - To as per OP's request.

Stevo3000
In the UK we don't use a "-" between code and number, thats a US localisation thing, nothing to do with us.
Pete Duncanson
Not all. It's variable, see Mark's answer
ChrisF
I'm UK too, but it was in the OP's question.
Stevo3000
Thanks stevo!, Which should I use first one or last one. Please also suggest which type to telephone number can be entered in this?
MKS
@ChrisF - I changed it as soon as I posted, remembered 0800. Mark's answer is not entirely correct, those numbers can be expressed as \d{4,5}\d{6}
Stevo3000
@Yuvraj - It depends on what you want to do with the number.
Stevo3000
This code doesn't handle metropolitan numbers. For instance, my phone number is in the format 020-8nnn-nnnn. Of course, some people insist on rendering that 0208-nnn-nnnn but they are wrong to do so, as only 020 is the area code.
APC
hi Steve what should I do for optional first 4,5,6 and also in ext we can have 3 and 4 optional.
MKS
I know that UK Number is not fixed but as per my requirement above regex is working for me. Thanks to all the members
MKS
@Yuvraj - Changes made
Stevo3000
So I can't use your website as I cannot paste in phone numbers from my address book because they do not meet some arbirary non standard.. Do you want to lose customers
Mark
This seems to be getting alot of down-votes considering it's programatically correct. Why down-vote the answer just because you don't like the question? I personally wouldn't validate phone numbers like this but I didn't ask the question.
Stevo3000
Because the regex does not deal with all cases e.g. numbers are quote with spaces - 020 9999 999 and that regex needs a - also the code for London has 3 digits for the STD code - also what use is a program that users cannot use?
Mark
@Mark - That is for you to take up with the OP, I'm just answering his question. He asked for a pattern to match his cases, if he hadn't said he was using it for telephone numbers would you be down voting?
Stevo3000
+1  A: 

The only reliable way to really validate a number is to try calling it.

(Just check its not 999 first.)

billpg
Or my premium rate sex line ;)
wefwfwefwe
A: 

I've used two Regex's in the past, one for landlines:

^(\+44)?\s?\(?0\)?[12358]\d\s?\d\)?\s?\d\)?(\s?\d){6}$

and one for mobile lines:

^(\+44)?\s?\(?0\)?7[5789]\d\s?\d\)?\s?\d\)?(\s?\d){5}$

Hopefully these will help point you in the right direction.

Lazarus
Thanks Laz, can I see the examples for above regex both mobile and landline so that I can check whether it is required to me or not.
MKS
does not deal with all numbers
Mark
What numbers does it not handle?
Lazarus
+1  A: 

You are on a looser here straight away. In the UK we don't write numbers like that anyway and we have a raft of "funny" rules and formats that we like to use. There really is no standard way to represent numbers here.

I really don't think RegExp is the way to go, not without making it silly long and even then you won't cover everything so another approach is needed I feel.

Some sample numbers (please don't take these as your use cases now RegExp lovers):

Most land line numbers start with 01 (others are available though)

(012345) 333444 012345 333444

Then there are preimum number or free phone numbers:

0800 333444 0849 333444

Or a mobile (cell to you guys over the pond)

07123 123456 07123123456 07123 123 456

Even the space is optional!

Then theres international numbers

01235 333222 can also be +44 12345 333222

So hopefully you can see there is simply too many variations to realistically create a RegExp to catch them all without it getting silly long and unreadable.

A better bet would be to strip out any funny characters and check the minimum length to ensure its over 10 (or close to it, your choice), thats about the best you can do and its nice and simple and if its wrong, its wrong. People can still mistype a phone number in anyway, theres only so much you can do in cases like this and being too strict could really annoy your user. What if they have a valid number thats not covered by your strict RegExp, how annoying would it be to be told that your home number is apparently "invalid"?

Pete Duncanson
Not all land line numbers begin with 01 - Greater London numbers start with 0207/8.
Daniel May
Proves me point nicely, too many combos, thanks Daniel ;)
Pete Duncanson
London's area code is technically 020 - I've worked at places with 0207, 0208 and 0203 codes: and 0204, 0205 etc are reserved for future expansion.
Richy C.
A: 

** I'm looking for a routine that will format a string of numbers as a UK phone number. **

You could download the Ofcom database that lists the formats for each number range, including national dialling only numbers, and do a lookup for each number you need to format. The database lists the SABCDE digits and the format: 0+10, 2+8, 3+7, 4+6, 4+5, 5+5, or 5+4 for each range.

There are a small number of errors in the database (especially for 01697 and 0169 77 codes), but they number less than ten errors in more than a quarter of a million entries.

There are four files covering 01 and 02 numbers, and separate files for various non-geographic number ranges.

0+10 numbers are 'National Dialling Only' and are written without parentheses around the area code part. The area code will be 02x for all 02 numbers, 01xx for all 011x and 01x1 numbers, and 01xxx for most other 01 numbers (a very small number - about a dozen - will be 01xx xx though).

Parentheses surround the area code on all other 01 and 02 numbers (that is, use parentheses on 01 and 02 numbers where the local number part does not begin with a 0 or a 1). Parentheses show that local dialling is possible within the same area by omitting the digits enclosed by the parentheses.

The 2+8 nomenclature shows the area code and local number length, with the entry 2075 : 2+8 meaning the number is formatted as (020) 75xx xxxx. Remember the leading zero is not 'counted' in the 2+8 determination.

** UK telephone numbers vary in length from 8 digits to 12 digits **

No. Since 2000, most have 10 digits after the '0' trunk code. A few still have 9 digits after the '0' trunk code.

There are also a few special numbers such as 0800 1111 and 0845 4647 to consider.

** "area" codes can vary between 2 and 4 digits. **

Area codes can vary between 2 and 5 digits (the leading zero is not counted). To be clear, '020' is classed as a 2-digit area code because the leading 0 is actually the trunk code. There are also 011x and 01x1 area codes, and most numbers others have 01xxx area codes. The latter may have local numbers that are only 5 digits long instead of the more widely found 6 digit local numbers. A very small number have an 01xx xx area code and these have 5 or 4 digit local numbers.

** Also, there's no standard for exactly where spaces are put. **

There is always a space between the area code part and the local number part for all 01 and 02 numbers.

It is also traditional for (01xx xx) area codes to have a space within the area code as shown. This represents the old local exchange groupings where this system is still in use. Other (shorter) area codes are not split.

Local numbers with 7 or 8 digits have a split before the fourth digit from the end. Local numbers with 4, 5, or 6 digits are not split. This applies to geographic and non-geographic numbers alike.

For most 03, 08, and 09 numbers, the number is written as 0xxx xxx xxxx.

Some 0800 and all 0500 numbers are written 0xxx xxxxxx.

For 055, 056, and 070 numbers the number is written 0xx xxxx xxxx.

For mobile and pager numbers, use 07xxx xxxxxx.

** except some people use '08000 abc def' instead of '0800 0abc def' **

That usage is incorrect. Do be aware that some 0800 numbers have 9 digits after the 0 trunk code, whilst others have 10 digits after the 0 trunk code.

So, both 0800 xxxxxx and 0800 xxx xxxx are correct.

0500 numbers use only 0500 xxxxxx.

Most 03, 08, and 09 numbers are written written as 0xxx xxx xxxx.

http://en.wikipedia.org/wiki/Local%5Fconventions%5Ffor%5Fwriting%5Ftelephone%5Fnumbers#United%5FKingdom

** UK phone numbers come in the format (12345) 123456 *

Incorrect, and too generalised. Trunk code is '0' and local numbers do not begin with a '1'.

** All land line numbers start with 01 **

Incorrect. Five areas have (02x) codes since 2000.

** (012345) 333444 or 012345 333444 **

Too many digits in both of those examples. UK numbers have a maximum of ten digits after the 0 trunk code.

** Unless its a premium number or free phone 0800 333444 or 0849 333444 **

** I changed it as soon as I posted, remembered 0800. Mark's answer is not entirely correct, those numbers can be expressed as \d{4,5}\d{6} **

No. 0800 numbers can be \d{4}\d{6,7} but 0808 and all other 08xx numbers are \d{4}\d{7} as are all 03 and 09 numbers.

Don't forget that 0500 numbers still exist and are \d{4}\d{6} as they are a digit shorter than normal.

nrs