We help IT Professionals succeed at work.

Check for Numeric Phone Field

Richard Korts
on
206 Views
Last Modified: 2017-05-15
I want to use Javascript to check an HTML form field for a legit US phone number.

I want to allow for numbers, -, (, ), and . AND I want the number of digits to be exactly 10.

If the field passes these criteria, it is OK & will be passed on as is.

Is there a simple Javascript or Jquery way to do this?

Thanks
Comment
Watch Question

HainKurtSr. System Analyst
CERTIFIED EXPERT

Commented:
have a look at this sample

https://jsfiddle.net/fhgd4ock/

HTML
<input id=phone>
<button onClick="javascript:test();">test</button>

Open in new window


CODE
function validPhone(phone_number) {
  phone_number = phone_number.replace(/\s+/g, "");
  return phone_number.length >= 10 && phone_number.match(/^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$/);
}

function test() {
  if (validPhone($("#phone").val())) {
    alert("yippu");
  } else {
    alert("oops!");
  };
}

Open in new window

HainKurtSr. System Analyst
CERTIFIED EXPERT

Commented:
you can find any regex and plug it to above code to match your needs...

I used

^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$

Open in new window

^\s*                #Line start, match any whitespaces at the beginning if any.
(?:\+?(\d{1,3}))?   #GROUP 1: The country code. Optional.
[-. (]*             #Allow certain non numeric characters that may appear between the Country Code and the Area Code.
(\d{3})             #GROUP 2: The Area Code. Required.
[-. )]*             #Allow certain non numeric characters that may appear between the Area Code and the Exchange number.
(\d{3})             #GROUP 3: The Exchange number. Required.
[-. ]*              #Allow certain non numeric characters that may appear between the Exchange number and the Subscriber number.
(\d{4})             #Group 4: The Subscriber Number. Required.
(?: *x(\d+))?       #Group 5: The Extension number. Optional.
\s*$                #Match any ending whitespaces if any and the end of string.

Open in new window


you can find many regex, just google "10 digit phone number regex"
Richard KortsBusiness Owner / Chief Developer

Author

Commented:
Not so good, rejects this phone # with AND without the dashes.

619-855-9192
HainKurtSr. System Analyst
CERTIFIED EXPERT

Commented:
it works with "619-855-9192" says yippuuu :)
Richard KortsBusiness Owner / Chief Developer

Author

Commented:
The jsfiddle fails all the time.

I tried to incorporate your code into my test. As follows.

<!DOCTYPE html>

<html>
<head>
<title>Untitled</title>
<script>
function validPhone(phone_number) {
  phone_number = phone_number.replace(/\s+/g, "");
  return phone_number.length >= 10 && phone_number.match(^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$);
}
function test() {
  if (validPhone($("#phone").text())) {
    alert("yippu");
  } else {
    alert("oops!");
  };
}	
</script>
</head>
<body>


<input id="phone">
<button onClick="javascript:test();">test</button>


</body>
</html>

Open in new window


Does nothing , click test, nothing.

I must have done something wrong.

Thnks
Richard KortsBusiness Owner / Chief Developer

Author

Commented:
I was using the jsfiddle version, I answered before your 2nd answer.

It doesn't work either.
HainKurtSr. System Analyst
CERTIFIED EXPERT

Commented:
include jquery

<script type="text/javascript" src="//code.jquery.com/jquery-git.js"></script>

Open in new window

Richard KortsBusiness Owner / Chief Developer

Author

Commented:
Still does not work. Does NOTHING.

Code now

<!DOCTYPE html>

<html>
<head>
<script type="text/javascript" src="//code.jquery.com/jquery-git.js"></script>
<title>Untitled</title>
<script>
function validPhone(phone_number) {
  phone_number = phone_number.replace(/\s+/g, "");
  return phone_number.length >= 10 && phone_number.match(^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$);
}
function test() {
  if (validPhone($("#phone").text())) {
    alert("yippu");
  } else {
    alert("oops!");
  };
}	
</script>
</head>
<body>


<input id=phone>
<button onClick="javascript:test();">test</button>


</body>
</html>

Open in new window

:
Richard KortsBusiness Owner / Chief Developer

Author

Commented:
Can someone else come in? This seems to be going nowhere.
HainKurtSr. System Analyst
CERTIFIED EXPERT

Commented:
why dont you copy paste the code from fiddle...
function validPhone(phone_number) {
  phone_number = phone_number.replace(/\s+/g, "");
  return phone_number.length >= 10 && phone_number.match(/^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$/);
}

function test() {
  if (validPhone($("#phone").val())) {
    alert("yippu");
  } else {
    alert("oops!");
  };
}

Open in new window


line 13

if (validPhone($("#phone").text())) {
>>>
if (validPhone($("#phone").val())) {

here is the fiddle link again

https://jsfiddle.net/fhgd4ock/
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
And the documentation for the above.Note that it has phoneUs property.Jquery validation plugin
CERTIFIED EXPERT
Top Expert 2010

Commented:
Unless you have specific requirements, you might as well go with the jQuery Validation library that's already been written.  As pointed out above, it has phone number validation built-in.   There's a demo on the site linked in Comment ID 42136045
Richard KortsBusiness Owner / Chief Developer

Author

Commented:
Perfect. I want to use it in one of a series of other form validations that all use Javascript alerts if an issue and DO NOT submit the form. I like the little blue checkmark & the red outlined box, but can I use my "old" methods as well? Otherwise, I have to convert all my other checks to this style (which is EXCELLENT), don't know if I want to go through the effort. Actually don't know how.

Thanks

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.