简体   繁体   English

电话号码验证html表格

[英]Phone number validation html form

I am a beginner in javascript and am not sure what is going wrong in the code below. 我是javascript的初学者,不确定下面的代码出了什么问题。 It doesn't show any alert when a wrong number is entered. 输入错误的号码时,它不会显示任何警报。 Thanks for the help.The html code for the the attribute is 感谢您的帮助。该属性的html代码为

<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. 函数中的大括号在此处不执行任何操作,并且if语句后没有任何语句(就此而言,不是语句),因此直接在其后的}会导致SyntaxError。 Did you simply mean: 您只是说:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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