View Full Version : Is dynamic validation for telephine number possible ?

05-03-2007, 06:41 PM

I wish to know if one can dynamically validate a textbox that accepts telephone numbers.

I plan to do this by:
first selecting a country form a dropdown box.
then validate the text box for the corresponding country selected...

but do not have any idea how to do it!

can you help me ?

05-03-2007, 07:59 PM
You have set yourself a pretty big task if you have no idea how to start. Maybe read some form validation tutorials/strategies first like this one



Philip M
05-03-2007, 08:30 PM
As with most other validations, over-elaboration of telephone number validation can be counter-productive.

The main risk is that the user simply enters the wrong number (transposed digits etc.) and nothing can prevent this.

I would advise that telephone number validation is simply a check that the input box contains only numbers (without specifying how many or their precise format which varies from country to country), and also + (international prefix), - (hyphens) and/or spaces.

- and space can be stripped out if necessary.

The regex is:-

if (/[^0-9\s\-\+]/gi.test(phoneNumber.value) || (phoneNumber.value.length <9 )) { /// only digits, space, hyphen and plus sign allowed. Minimum of 9 characters.

05-03-2007, 09:04 PM
The regular expression I usually use for phone numbers is:

/^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3})|(\(?\d{2,3}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/

This should allow for most local and international formats that don't involve using letters to represent the numbers.

Philip M
05-03-2007, 09:39 PM
Stephen -

That rejects my number which is something like 01483 674960

01483 is the exchange, 674960 is the 6-digit subsciber number.

Also rejects ++44 (international dialing code). In UK the ++ is replaced by 00.

I prefer my simpler check!

05-04-2007, 08:28 AM
Thank a lot Philip M for your valuable suggestion.
I too was thinking on those lines.

05-04-2007, 11:39 AM
The normal international convention is a single + to represent the international code otherwise I'd have set it up to accept ++++ since the international code here is four digits long.

It would also be more normal to break a number up more with spaces to make it easier to read. three or four digits at a time is about all anyone would normally say without a pause. I had forgotten that there were still places using hundreds of area codes for the same town but you could fix that simply by allowing for more digits in each group.

Philip M
05-04-2007, 05:26 PM
Most UK numbers are a five- (or possibly four-) digit area code (starting with 0) followed by a six-digit subscriber number. 0xxxx xxxxxx

But big towns and cities including London take the form 020 xxxx xxxx with either a space or a hyphen between the groups.

There are not "hundreds of area codes for each town" but a single area code may encompass several medium sized towns. For example, 01483 is Guildford but extends to places such as Cranleigh, Woking and so on, say lesser towns within 15 miles radius.

10-20-2011, 05:12 PM
This other thread now contains a more detailed description for the entire UK number range: http://www.codingforums.com/showthread.php?t=8307

Hope it is useful!