###

Question:

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!

###

Solution:1

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.

###

Solution:2

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

###

Solution:3

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

###

Solution:4

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_conventions_for_writing_telephone_numbers#United_Kingdom

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.

###

Solution:5

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

(Just check its not 999 first.)

###

Solution:6

You are on a loser 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, 02)

(01234) 333444 and 01234 333444 are common

Then there are preimum number or free phone numbers:

0800 333444 or 0808 333 4444

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

07723 123456 or 07123123456 or 07723 123 456

Even the space is optional!

Then theres international numbers

01235 333222 can also be +44 1235 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"?

###

Solution:7

As several other commentators have pointed out, the area code may be 3, 4, or 5 numbers long.

After the leading 0 trunk prefix or +44 country code, the area code can be 2, 3, 4 or 5 digits long. The total number length (not including the 0 or +44) can be 10 or 9 digits.

With a 2 digit area code the subscriber number is 8 digits long.

With a 3 digit area code the subscriber number is 7 digits long.

With a 4 digit area code the subscriber number can be 6 or 5 digits long.

With a 5 digit area code the subscriber number can be 5 or 4 digits long.

Some 0800 and all 0500 numbers are also a digit shorter than normal.

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 There's still a problem with that pattern. You'll get 0800 445566 just fine, but for longer numbers 08001 334455 and 08009 667788 are not OK. Those should be 0800 133 4455 and 0800 966 7788. Note that 0808 numbers are always formatted as 0808 555 7777 as they always have 10 digits after the initial 0.

###

Solution:8

Try this:

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

'

###

Solution:9

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.

**Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com**

EmoticonEmoticon