0

在下面的代码中,有一个为新用户创建登录的表单。在按下继续按钮时,它应该调用“validateForm()”函数。在检查表格中填写的值以及缺少正确值的情况下,它应该发出警报消息。但是,即使出现错误也不会发出任何警报,就像我按下“继续”一样,即使表格完全是空的。

<html>
<head>
<title></title>
<script type="text/javascript">
function validateForm()
{
    var email=document.forms["newlogin"]["email"].value;
    var cemail=document.forms["newlogin"]["cemail"].value;
    var pass=document.forms["newlogin"]["pwd"].value;
    var cpass=document.forms["newlogin"]["cpwd"].value;
    var answ1=document.forms["newlogin"]["ans1"].value;
    var answ2=document.forms["newlogin"]["ans2].value;

    alert("I am IN");

    if(email==null || email=="" || cemail==null || cemail=="") {
        alert("Enter Email ID and confirm.");
        return false;
    }
    if(pass==null || pass=="" || cpass==null || cemail=="") {
        alert("Enter Password and confirm.");
        return false;
    }
    if(answ1==null || answ1=="" || answ2=null || answ2=="") {
        alert("Enter Answer 1 & Answer 2.");
        return false;
    }
    if(email!=cemail) {
        alert("Confirmed Email ID is not matching.");
        return false;
    }
    if(pass!=cpass) {
        alert("Confirmed Password is not matching.");
        return false;
    }
}
</script>
</head>


<body>
<h1 style="font:swis721 bt">enginenc</h1>
<form name="newlogin" method="POST" action="http://localhost/cgi-bin/createlogin" onsubmit="return validateForm()">
    <table align="center" cellpadding="10" cellspacing="0" style="margin-top:2cm;background-color:#FFD700;border-top:1px solid;border-bottom:1px solid;border-left:1px dotted;border-right:1px dotted" border=0>
        <tr>
            <td align="left" style="font:westwood let">Enter a valid Email ID:</td>
            <td align="left" style="font:westwood let"><input type="text" name="email"></td>
        </tr>
        <tr>
            <td align="left" style="font:westwood let">Confirm the Email ID:</td>
            <td align="left" style="font:westwood let"><input type="text" name="cemail"></td>
        </tr>
        <tr>
            <td align="left" style="font:westwood let">Enter password:</td>
            <td align="left" style="font:westwood let"><input type="password" name="pwd"></td>
        </tr>
        <tr>
            <td align="left" style="font:westwood let">Confirm Passowrd:</td>
            <td align="left" style="font:westwood let"><input type="password" name="cpwd"></td>
        </tr>
        <tr>
            <td align="left" style="font:westwood let">Select Question 1:</td>
            <td align="left" style="font:westwood let"><select name="ques1"><option value=1>1. What is your mother's maiden name?</option><option value=2>2. Which is your birth city?</option><option value=3>3. Which is your favorite place?</option></select></td>
        </tr>
        <tr>
            <td align="left" style="font:westwood let">Answer 1:</td>
            <td align="left" style="font:westwood let"><input type="text" name="ans1"></td>
        </tr>
        <tr>
            <td align="left" style="font:westwood let">Select Question 2:</td>
                        <td align="left" style="font:westwood let"><select name="ques2"><option value=1>1. Which is your dream car?</option><option value=2>2. What is your nick name?</option><option value=3>3. Who is your favorite singer?</option></select></td>
        </tr>
        <tr>
                        <td align="left" style="font:westwood let">Answer 2:</td>
                        <td align="left" style="font:westwood let"><input type="text" name="ans2"></td>
                </tr>
        <tr>
            <td align="left" style="font:westwood let"><input style="margin-left:3cm;padding:11px" type="submit" value="Continue"></td>
            <td align="left" style="font:westwood let"><input style="padding:11px" type="reset"></td>
        </tr>
    </table>
</form>
</body>
</html>

在这里,使用onsubmit()="return validateForm()"函数提交表单。Form 没有调用函数validateForm()。我尝试了很多但没有发现错误。请帮忙。谢谢。

4

4 回答 4

0

你有错别字:

var answ2=document.forms["newlogin"]["ans2].value;- 失踪 ”

if(answ1==null || answ1=="" || answ2=null || answ2=="")- 失踪==

然后它应该工作。

于 2013-01-28T09:55:36.143 回答
0

改变

var answ2=document.forms["newlogin"]["ans2].value;

var answ2=document.forms["newlogin"]["ans2"].value; //missing quote

if(answ1==null || answ1=="" || answ2=null || answ2=="") {

if(answ1==null || answ1=="" || answ2==null || answ2=="") {
于 2013-01-28T09:55:42.960 回答
0

有一些语法错误。

检查这条线。"该行中缺少。更改行从,

var answ2=document.forms["newlogin"]["ans2].value;

到,

 var answ2=document.forms["newlogin"]["ans2"].value;

而且,您必须使用==if 语句检查条件。但在下面一行中,您只使用了一个=. 换行,

if(answ1==null || answ1=="" || answ2=null || answ2=="") {
        alert("Enter Answer 1 & Answer 2.");
        return false;
    }

到,

if(answ1==null || answ1=="" || answ2==null || answ2=="") {
        alert("Enter Answer 1 & Answer 2.");
        return false;
}

建议:使用 firebug 或 javascript 错误控制台找出这些错误。

于 2013-01-28T09:58:55.357 回答
0

尝试在调用函数中添加“ this ”:<form name="newlogin" method="POST" action="http://localhost/cgi-bin/createlogin" onsubmit="return validateForm(this)">

于 2013-01-28T09:59:52.123 回答