Validating USA phone numbers

Asked
Active3 hr before
Viewed126 times

5 Answers

numbersvalidatingphone
90%

Possible Duplicate: A comprehensive regex for phone number validation Validate phone number with JavaScript , 29 @HaBo: A regex can't detect valid phone numbers, only validly formatted phone numbers. That said, no U.S. area codes begin with 0 or 1, so if you wanted, you could change the initial [0-9]{3} to [2-9][0-9][0-9]. – ruakh Nov 26 '13 at 23:31 ,I'm trying to write a regular expression to validate US phone number of format (123)123-1234 -- true 123-123-1234 -- true, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

The easiest way to match both

^ \([0 - 9] {
   3
}\)[0 - 9] {
   3
} - [0 - 9] {
   4
}
$
load more v
88%

Area codes start with a number 2–9, followed by 0–8, and then any third digit.,The second group of three digits, known as the central office or exchange code, starts with a number 2–9, followed by any two digits.,Recipe 4.3 shows how to validate international phone numbers.,The final four digits, known as the station code, have no restrictions.

^ \( ? ([0 - 9] {
   3
})\) ? [-.●] ? ([0 - 9] {
   3
})[-.●] ? ([0 - 9] {
   4
}) $
load more v
72%

Save & share expressions with others.,Roll over a match or expression for details.,Results update in real-time as you type.,Favicon.ico & App Icon Generator

{
   {
      getLabel()
   }
}
load more v
65%

The validating phone number is an important point while validating an HTML form. In this page we have discussed how to validate a phone number (in different format) using JavaScript : At first, we validate a phone number of 10 digits with no comma, no spaces, no punctuation and there will be no + sign in front the number. Simply the validation will remove all non-digits and permit only phone numbers with 10 digits. Here is the function. ,Validate an international phone number with country code ,At first we validate a phone number of 10 digit. For example 1234567890, 0999990011, 8888855555 etc. ,Validate a 10 digit phone number

The validating phone number is an important point while validating an HTML form. In this page we have discussed how to validate a phone number (in different format) using JavaScript :

At first, we validate a phone number of 10 digits with no comma, no spaces, no punctuation and there will be no + sign in front the number. Simply the validation will remove all non-digits and permit only phone numbers with 10 digits. Here is the function.

function phonenumber(inputtxt) {
   var phoneno = /^\d{10}$/;
   if ((inputtxt.value.match(phoneno)) {
         return true;
      } else {
         alert("message");
         return false;
      }
   }

To valid a phone number like
XXX-XXX-XXXX
XXX.XXX.XXXX
XXX XXX XXXX
use the following code.

function phonenumber(inputtxt) {
   var phoneno = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
   if ((inputtxt.value.match(phoneno)) {
         return true;
      } else {
         alert("message");
         return false;
      }
   }

If you want to use a + sign before the number in the following way
+XX-XXXX-XXXX
+XX.XXXX.XXXX
+XX XXXX XXXX
use the following code.

  function phonenumber(inputtxt) {
     var phoneno = /^\+?([0-9]{2})\)?[-. ]?([0-9]{4})[-. ]?([0-9]{4})$/;
     if ((inputtxt.value.match(phoneno)) {
           return true;
        } else {
           alert("message");
           return false;
        }
     }

CSS Code

 li {
    list - style - type: none;
    font - size: 16 pt;
 }
 .mail {
    margin: auto;
    padding - top: 10 px;
    padding - bottom: 10 px;
    width: 400 px;
    background: #D8F1F8;
    border: 1 px soild silver;
 }
 .mail h2 {
    margin - left: 38 px;
 }
 input {
    font - size: 20 pt;
 }
 input: focus, textarea: focus {
    background - color: lightyellow;
 }
 input submit {
    font - size: 12 pt;
 }
 .rq {
    color: #FF0000;
    font - size: 10 pt;
 }

HTML Code

<!DOCTYPE html>
<html lang="en">

<head>
   <meta charset="utf-8">
   <title>JavaScript form validation - checking non-empty</title>
   <link rel='stylesheet' href='form-style.css' type='text/css' />
</head>

<body onload='document.form1.text1.focus()'>
   <div class="mail">
      <h2>Input an Phone No.[xxxxxxxxxx] and Submit</h2>
      <form name="form1" action="#">
         <ul>
            <li><input type='text' name='text1' /></li>
            <li>&nbsp;</li>
            <li class="submit"><input type="submit" name="submit" value="Submit" onclick="phonenumber(document.form1.text1)" /></li>
            <li>&nbsp;</li>
         </ul>
      </form>
   </div>
   <script src="phoneno-all-numeric-validation.js"></script>
</body>

</html>

JavaScript Code

function phonenumber(inputtxt) {
   var phoneno = /^\d{10}$/;
   if (inputtxt.value.match(phoneno)) {
      return true;
   } else {
      alert("Not a valid Phone Number");
      return false;
   }
}

HTML Code

<!DOCTYPE html>
<html lang="en">

<head>
   <meta charset="utf-8">
   <title>JavaScript form validation - checking non-empty</title>
   <link rel='stylesheet' href='form-style.css' type='text/css' />
</head>

<body onload='document.form1.text1.focus()'>
   <div class="mail">
      <h2>Input an Phone No.[xxx-xxx-xxxx, xxx.xxx.xxxx, xxx xxx xxxx] and Submit</h2>
      <form name="form1" action="#">
         <ul>
            <li><input type='text' name='text1' /></li>
            <li>&nbsp;</li>
            <li class="submit"><input type="submit" name="submit" value="Submit" onclick="phonenumber(document.form1.text1)" /></li>
            <li>&nbsp;</li>
         </ul>
      </form>
   </div>
   <script src="phoneno-international-format.js"></script>
</body>

</html>

JavaScript Code

function phonenumber(inputtxt) {
   var phoneno = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
   if (inputtxt.value.match(phoneno)) {
      return true;
   } else {
      alert("Not a valid Phone Number");
      return false;
   }
}

HTML Code

<!DOCTYPE html>
<html lang="en">

<head>
   <meta charset="utf-8">
   <title>JavaScript form validation - checking non-empty</title>
   <link rel='stylesheet' href='form-style.css' type='text/css' />
</head>

<body onload='document.form1.text1.focus()'>
   <div class="mail">
      <h2>Input an Phone No.[+xx-xxxx-xxxx, +xx.xxxx.xxxx, +xx xxxx xxxx] and Submit</h2>
      <form name="form1" action="#">
         <ul>
            <li><input type='text' name='text1' /></li>
            <li>&nbsp;</li>
            <li class="submit"><input type="submit" name="submit" value="Submit" onclick="phonenumber(document.form1.text1)" /></li>
            <li>&nbsp;</li>
         </ul>
      </form>
   </div>
   <script src="phoneno-+international-format.js"></script>
</body>

</html>

JavaScript Code

function phonenumber(inputtxt) {
   var phoneno = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
   if (inputtxt.value.match(phoneno)) {
      return true;
   } else {
      alert("Not a valid Phone Number");
      return false;
   }
}
load more v
75%

Advanced phone number validation using the most up-to-date international numbering plans, carrier & line type detection, and location lookup » ,Our format validation system is powered by regularly updated international phone numbering plans.,Our powerful, cost-effective phone number validation & lookup API is preventing undelivered messages for businesses all over the world.,Identify any national and international phone number simply by passing it into the API's request URL.

Valid
Local Format 4158586273
Intl. Format +14158586273
Country United States of America
Location Novato
Carrier AT&T Mobility LLC
Line Type Mobile
{
   "valid": true,
   "local_format": "4158586273",
   "intl_format": "+14158586273",
   "country_code": "US",
   "country_name": "United States of America",
   "location": "Novato",
   "carrier": "AT&T Mobility LLC",
   "line_type": "mobile"
}
load more v

Other "numbers-validating" queries related to "Validating USA phone numbers"