繁体   English   中英

单击“确认”后没有出现警告窗口

[英]No alert window appears after clicking 'Confirm'

这是我的作业,这是我第一次进行表单验证。以下代码分别是我的HTML代码和JavaScript代码。

HTML代码:

<html>

<head>
    <script src="validate_form.js" type="text/javascript"></script>
</head>

<body>
    <table>
        <form name="register" method="post" action="">
            <tr><td>First Name:</td><td><input type="text" id="firstname"/></td></tr>
            <tr><td>Surname:</td><td><input type="text" id="surname"/></td></tr>
            <tr><td>Gender:</td><td><input type="text" id="gender"/></td></tr>
            <tr><td>Birthday - Day:</td><td><input type="text" id="dob"/></td></tr>
            <tr><td>Month:</td><td><input type="text" id="month"/></td></tr>
            <tr><td>Year:</td><td><input type="text" id="year"/></td></tr>
            <tr><td>Yahoo ID:</td><td><input type="text" id="yahoo_id"/></td></tr>
            <tr><td>Password:</td><td><input type="text" id="password"/></td></tr>
            <tr><td>Retype password:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
            <td><input type="text" id="retype"/></td></tr>
            <tr><td>Question 1:</td><td><input type="text" id="q1"/></td></tr>
            <tr><td>Answer 1:</td><td><input type="text" id="a1"/></td></tr>
            <tr><td>Question 2:</td><td><input type="text" id="q2"/></td></tr>
            <tr><td>Answer 2:</td><td><input type="text" id="a2"/></td></tr>
            <tr><td><input type="submit" value="Confirm" onclick="validate_form ()"/></td><td></td></tr>
        </form>
    </table>
</body>



</html>

JavaScript代码:

function validate_form () 

{
    var first = document.getElementById("firstname").value;  /*get value from the IDs*/
    var sur = document.getElementById("surname").value;
    var sex = document.getElementById("gender").value;
    var day = document.getElementById("dob").value;
    var month = document.getElementById("month").value;
    var year = document.getElementById("year").value;
    var yahooid = document.getElementById("yahoo_id").value;
    var ps = document.getElementById("password").value;
    var retype_ps = document.getElementById("retype").value;
    var ques1 = document.getElementById("q1").value;
    var ans1 = document.getElementById("a1").value;
    var ques2 = document.getElementById("q2").value;
    var ans2 = document.getElementById("a2").value;

    var error = "";                              /*Regular expressions for matching purpose*/
    var val_name = /^[a-zA-Z]{1,}$/;
    var val_dob = /^[0-9]$/;
    var val_yahoo = /^[a-zA-Z0-9_]$/;
    var val_pass = /^[a-zA-Z0-9]$/;

    if (first == null || surname == null || !val_name.test(first)  || !val_name.test(sur) ) /*Validate firstname and surname*/                   

    {
        error += "Invalid firstname and/or surname.\n";
    }

    else if (sex == null) /*Validate gender*/ 

    {
        error += "Invalid gender.\n";
    }

    else if  (day == null || !val_dob.test(day)) /*Validate day of birth*/ 

    {
        error += "Invalid day of birth.\n";
    }

    else if  (month == null) /*Validate month of birth*/ 

    {
        error += "Invalid month of birth.\n";
    }

    else if (year == null || !val_dob.test(year)) /*Validate year of birth*/ 

    {
        error += "Invalid year of birth.\n";
    }

    else if (yahooid == null || !val_yahoo.test(yahooid)) /*Validate Yahoo Id*/ 

    {
        error += "Invalid yahoo Id.\n";
    }

    else if (ps == null || !val_pass.test(ps)) /*Validate password entered*/ 

    {
        error += "Invalid password.\n";
    }

    else if (retype_ps == null || retype_ps != password) /*Validate retype password*/ 

    {
        error += "Retype password is different form password.\n";
    }

    else if (ques1 == null || ans1 == null || ques2 == null || ans2 == null) /*Validate question 1, answer 1, question 2, and answer 2*/ 

    {
        error += "Invalid Question 1 and/or Answer 1 and/or Question 2 and/or Answer 2.\n";
    }

    else if (error == "") /*If no error, display the following message*/ 

    {
        alert("Thank you for your registration.");
    }

    else /*If there are errors, display all the error messages*/

    {
        alert(error);
    }
}   

那么,现在问题是代码有什么问题,直到警报窗口没有出现?希望错误可以被彻底指出,因为我还是JavaScript的新手。

当您在变量error连接消息时, else if作为独立的if必须执行最后的else if

...
else if (ques1 == null || ans1 == null || ques2 == null || ans2 == null)
{
  error += "Invalid Question 1 and/or Answer 1 and/or Question 2 and/or Answer 2.\n";
}

if (error == "") /*If no error, display the following message*/ 
{
  alert("Thank you for your registration.");
}
else /*If there are errors, display all the error messages*/
{
  alert(error);
}

注意:我还注意到在检查密码重复时是否存在错误,您已更改其名称(您将其命名为password而不是ps )。

我在JSFiddle中上传了两个示例: http ://fiddle.jshell.net/kcDBU/

这其中只有if不是else ifhttp://fiddle.jshell.net/kcDBU/1/

在Javascript中,您有一些被视为错误的值:

  • 假,
  • 空值,
  • 不确定的,
  • ',
  • 0,
  • NAN;

你要考虑你的价值也可能是“未定义的”,所以,如果我是你,我会这样做:

if (!first || !surname || !val_name.test(first)  || !val_name.test(sur) )                   
{
    error += "Invalid firstname and/or surname.\n";
}

并将您的脚本放在body标签的末尾。

暂无
暂无

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

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