Javascript验证无效。没有提出警报

时间:2014-02-13 13:25:50

标签: javascript validation

运行此代码时未引发任何警报。它不起作用。我做错了什么?

这是我的代码:
我也尝试过document.formname.value但仍然没有结果。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
    <title>Regestration form</title>
    <link  rel="stylesheet" href="style.css" type="text/css" />
    <script >
function validate()

{
    if (document.getElementsByName(fname) = "")
    {
        alert (" please enter first name ");

        return false ;

        }
    if (document.getElementsByName(lname) = "")
    {
        alert (" please enter last name ");

        return false ;

        }
    if (document.getElementsByName('gen') = "")
    {
        alert (" please select gender ");

        return false ;

        }

{   
var eid = documnet.getElementsByName(email);
var atpos = eid.indexOf("@");
var dotpos = eid.lastIndexOf(".");
}
    if (document.getElementsByName(email)= "" ||atpos < 1 || (atpos-dotpos < 2)
    {
        alert (" please enter a valid email ");

        return false ;

        }
    if (document.getElementsByName(pass1) = "" || document.getElementsByName(pass1) < 6)
    {
        alert (" Please enter atleast 6 digit password ");

        return false ;

        }
    if (document.getElementsByName(pass1)!= document.getElementsByName(pass2))
    {
        alert ("password do not match");

        return false ;
    }
}
    </script>
    </head>

    <body>
    <?php
    require 'formprocessor.php';


    ?>
    <div id="frm">
    <div id="fhead">
    <h3>Registeration Form</h3>
    </div>
    <form method="post" name="regfrm" action="formprocessor.php" onsubmit="return validate();" >
    <div class="name">First Name</div>
    <div class="input"><input type="text" name="fname"  placeholder="Enter First Name"/> <span><?php echo $errfn ?></span></div>
    <div class="clear"></div>
    <div class="name">Last Name</div>
    <div class="input"><input type="text" name="lname" placeholder="Enter Last Name" /><span><?php echo $errln ?></span></div>
    <div class="clear"></div>
    <div  class="name">Gender</div>
    <div class="input"> <input type="radio" name="gen" value="male" />Male
                        <input type="radio" name="gen" value="female"/>Female
    <span><?php echo $errgn ?></span></div>
    <div class="clear"></div>
    <div class="name">Email</div>
    <div class="input"><input type="email" name="email" placeholder="Enter Valid Email"/><span><?php echo $errem ?></span></div>
    <div class="clear"></div>
    <div class="name">Password</div>
    <div class="input"><input type="password" name="pass1" placeholder="Minimum 6 character password"/><span><?php echo $errp1 ?></span></div>
    <div class="clear"></div>
    <div class="name">Confirm Password</div>
    <div class="input"><input type="password" name="pass2" placeholder="Same password as Above"/><span><?php echo $errp2 ?></span></div>
    <div class="clear"></div>
    <div id="sub" > <input type="submit" value="Register Now!" name="sub"  /></div>
    </form>
    </div>
    </body>
    </html>

1 个答案:

答案 0 :(得分:0)

if (document.getElementsByName(email)= "" ||atpos < 1 || (atpos-dotpos < 2)

在这里你有一个支架)在最后丢失。使用像notepad ++这样的编辑器进行检查。

{var eid = documnet.getElementsByName(email);var atpos = eid.indexOf("@");var dotpos =eid.lastIndexOf(".");}

这里不需要{}这只是添加

var eid = documnet.getElementsByName(email);var atpos = eid.indexOf("@");var dotpos = id.lastIndexOf(".");

最终

将所有document.getElementsByName(lname)替换为document.getElementsByName("fname")[0].value

相应更改名称,正确的名称在引号document.getElementsByName(“fname”)中归档,返回名称为“fname”的元素数组,因为我们只选择了一个具有该名称的字段,我们选择了document.getElementsByName(“fname” )[0] array的第一个索引。现在我们得到了第一个归档。现在我们必须使用document.getElementsByName(“fname”)[0] .value

从中获取值

http://www.w3schools.com/js/default.asp

了解有关javascript和html基础知识的更多信息