JavaScript phone numbers and cell phone number regular validation analysis

Source: Internet
Author: User
Tags regular expression

Here are some of the simplest telephone number verification, which only determines that the user entered the data can be passed this is very problematic

The code is as follows Copy Code

function Number_validator (TestValue)
{
var Checkok = "0123456789";
var checkstr = TestValue;
var allvalid = true;
for (i = 0;  i < checkstr.length; i++)
{
ch = checkstr.charat (i);
for (j = 0;  J < Checkok.length; J + +)
if (ch = = Checkok.charat (j))
Break
if (j = = Checkok.length)
{
return (false);
}
}
return (true);
}

such as user input 123456789 is this the phone, then we look at another call to verify the code


Because the method is the same, just write the relevant regular expression
For mobile phone number verification (provided in two ways)
var mobile=/^ ((13[0-9]{1}) |159|153) +d{8}$/;
var mobile1=/^ (13+d{9}) | (159+d{8}) | (153+d{8}) $/;


Verify the mobile phone number (check the 13,15,18 of the beginning of the phone number!) )

  code is as follows copy code
  function Check_telephone (obj) { 
     var reg=/^[1][358]d{9}$/; 
  & nbsp;  if (obj.value!= "&&!reg.test (obj.value)) { 
          alert (' Phone number format input Error! '); 
         obj.value = ""; 
          obj.focus (); 
         return false;  
    } 
 } 

//Validation for area code
 var phoneareanum =/^d{3,4}$/
//Authentication for phone number
 var phone =/^d{7,8}$/;

  code is as follows copy code
function check _phone (obj) { 
     var reg=/^ ([0+]d{2,3}-)? 0d{2,3})? (d{7,8}) (-(D{3,})?$/;  
     if (obj.value!= "" &&!reg.test (obj.value)) { 
         alert (' Phone number format input Error! '); 
         obj.value = ""; 
          obj.focus (); 
         return false;  
    } 
 } 

Verifying fixed Phone numbers
0d{2,3} represents the area code
[0+]d{2,3} represents the international area code
d{7,8} represents 7-8 digits (for phone numbers)
Correct format: Area code-Phone number-extension number (all write | write phone number only)


Call method We just add onsubmit= ' return to the form (Checkinput (this)) '

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.