Javascript - Validating international phone numbers

How do I validate international phone number in my lead form on my site?


  • 3717 views
Share Send to a friend Watch Report
 
 

Posted Answers

Order by
 
9 thumbs up

OK, this one is very tricky. Some people decide that the field should have digits only. This is a very user-unfriendly way of handling forms. I usually don't validate phone numbers at all, for the simple reason that you cannot say for sure that it is _a phone number_. All you know is that it is in a certain format. Phone numbers can be notated in so many ways that validation is practically impossible.

If you really wish to validate something, you can either state that it should contain only numbers, or you can parse the string, and make sure the prefix starts with 00-countryCode, although a "+" is also valid. The country codes are very well known and can be looked up in a list.

In short I advise against this. Try to lookup usability advice. Maybe http://useit.com will help.

Quote from useit.com: For phone numbers, allow for international numbers containing a varying number of digits and a country code. Also, avoid stating your own phone number in letters (say, 1-800-TOO-EASY) because many countries don't include letters in their telephone keypads.


Posted 2 years ago ( permalink )
In reply to boazbinnun's question
blonkm was invited by Yedda to answer this question.

Rated as
#2 out of 3
0
3

Helpful?

line
line
line



 

To do this, you can get a Regular Expression that matches international phone numbers and then you perform a match against the supplied phone number.

A useful source of regular expressions is on the Regular Expressions Library, which has a section on phone numbers and addresses:
http://regexlib.com/DisplayPatterns.aspx?cattabindex=6&categoryId=7

I can see three there that may work for you:
http://regexlib.com/REDetails.aspx?regexp_id=1395
http://regexlib.com/REDetails.aspx?regexp_id=296
http://regexlib.com/REDetails.aspx?regexp_id=688

 

For example:

var phone = '(305) 555-1234';

var regex = / ^(1\s*[-\/\.]?)?(\((\d{3})\)|(\d{3}))\s*
([\s-./\\])?([0-9]*)([\s-./\\])?([0-9]*)$/;

if (phone.match(regex)){
  /* It matches! */ }

else{
  /* It doesn't! */ }

 

See also http://www.regular-expressions.info for a good reference on regular expressions. 


Posted 2 years ago ( permalink )
In reply to boazbinnun's question
Premasagar was invited by Yedda to answer this question.

Rated as
Best Answer
0
7

Helpful?

line
line
line



 

i want phone number validation in java script


Posted 8 months ago ( permalink )
In reply to boazbinnun's question
Rated as
#3 out of 3
0
0

Helpful?

line
line
line



Sign in to participate

Got an answer for boazbinnun? Would you like to comment on the posted answers, or vote for the one which you think is the best?

Sign up for a free account, or sign in (if you're already a member).

Explore Related Questions

Other people asked questions on similar topics, check out the answers they received:


Callback in url

I have written a json file which I want to sent to a javascript file I want to use callback in url to accomplish this. But I am ...
Submitted by dharamveer 1 month ago
  • viewed 20 times

Last answer posted 28 days ago by dharamveer


Feed - Subscribe to changes to this Q&A Blog

Explore Related Videos

JavaScript External File

This screencast is going to talk about attaching external JavaScript files to your HTML document. You can put your common JavaScript code in a separate JavaScript document so you can link to this JavaScript file from all your HTML page. This way, you can reuse your code over an over again.

JavaScript Big Picture

In this screencast, we are going to look at the big picture of what javascript is. After this tutorial video, you should be able to see how JavaScript fit into your web development process.

JavaScript Events General

When youre coding JavaScript, you are building functions that respond to user actions (which is also known as events) on your webpage. Therefore, event is the driving force for JavaScript to work. This screencast is going to look at events in general.

What is JavaScript?

In this screencast, we are going to dive into the world of a very popular scripting language called JavaScript. JavaScript is used on most major portal websites today and its major use is to make your static HTML + CSS webpage more dynamic and eventful.

How to start using JavaScript?

This screencast is going to show you how to start using JavaScript in your HTML pages.

firebug tutorial

A tutorial of how to use firebug to debug javascript, HTML, and CSS.

SendTo Clipboard Screencast

How the SendTo menu, the clipboard and javascript: url can easily bring closer different applications.More details and link to download at http://xdexavier.blogspot.com/2007/08/sendto-clipboard-coolness-tiddlywiki.html

Greasemonkey

In this video, I demonstrate Greasemonkey for Firefox. This popular add-on allows you to manipulate web pages via client side javascript. What does that mean? It means you can change a way a page looks, feels and operates without affecting anyone else. It's a great mechanism for customizing the web the way you want. To install Greasemonkey, to go http://addons.mozilla.org and search for "Greasemonkey". After you've installed it, head over to http://userscripts.org and install some of the popular scripts there. Enjoy!

Simple Pagination with Javascript and HTML

Segment from Hak5 1x06 - Simple Pagination with Javascript and HTML Distributed by Tubemogul.

JavaScript - 02 - Using JavaScript

For more Java programming tutorials visit: http://www.programmingvideotutorials.com
» More...
Powered by