简体   繁体   中英

Phone number validation html form

I am a beginner in javascript and am not sure what is going wrong in the code below. It doesn't show any alert when a wrong number is entered. Thanks for the help.The html code for the the attribute is

<li><label for="phonenumber">Phone:</label></li>
<li><input type="tel" name="phonenumber" /></li>

Validation code:

function formValidation()  
{  
    var uphone = document.registration.phonenumber;
    {
        if(ValidatePhone(uphone))
    }
    return false;
}

function ValidatePhone(uphone)  
{  
    var phoneformat = /(^\d{3}-\d{3}-\d{4})$/;  
    if(uphone.value.match(phoneformat))  
    {  
        return true;  
    }  
    else  
    {  
        alert('You have entered an invalid phone number!');  
        uphone.focus();  
        return false;  
    }  
}

This function is one big syntax error (which would prevent the whole script from running):

function formValidation()  
{  
    var uphone = document.registration.phonenumber;
    {
        if(ValidatePhone(uphone))
    }
    return false;
}

The inner braces in the function don't do anything here and the if statement has no statement (or block, for that matter) following it, so the } directly following it would cause a SyntaxError. Did you simply mean:

function formValidation()  
{  
    var uphone = document.registration.phonenumber;
    ValidatePhone(uphone);
    return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM