JavaScript Phone number validation

Source: Internet
Author: User

JavaScript Tutorial Phone number verification


The following check function, if the phone number is valid. At first we used the regular expression and the replace () method

To clear any interval character. Next, we use the isNaN () function to check that the phone number contains only numbers

。 Finally we check the string length and license, with only 10 digits of the phone number.


function Validatephone (FLD) {
var error = "";
var stripped = Fld.value.replace (/[().-]/g, ");

if (Fld.value = = "") {
Error = "You didn ' t enter a phone NUMBER.N";
Fld.style.background = ' yellow ';
else if (isNaN (parseint (stripped)) {
Error = "The phone number contains illegal CHARACTERS.N";
Fld.style.background = ' yellow ';
Or else if (! Stripped.length = = 10)) {
Error = "The phone number is the wrong length. Make sure

Included an area CODE.N ";
Fld.style.background = ' yellow ';
}
Return
Error
}

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.