表单在重定向到另一个页面之前不会读取javascript

时间:2017-12-07 04:55:41

标签: javascript html

我有一个表单,在将数据提交到另一个页面之前,它需要首先验证输入。但问题是表单重定向到另一个页面而没有先读取javascript。

<html>
<script>
    function fnValidate(){
     with document.frmAdd{
    var x;
    x = txtEmail.value;

    if( x.indexof("@")== -1)
    {   alert("A valid email address must contains @"); 
    return false; }
    if( isNaN(txtAge.value)== 1- )
    {   alert("Age must be filled out by number");
    return false; }
    if( txtPwd.value <> txtPwd2.value )
    {   alert("Password not matched!");
    return false; }

     } //end_with
    } //end_fnValidate()

</script>
<body>
    <form name="frmAdd" method="post" action="SaveNewUser.php" onSubmit="return fnValidate()">
        IC No    : <input type=text name="txtIc" size=20 required> <br>
        Name     : <input type=text name="txtName" size=50 required> <br>
        Email    : <input type=text name="txtEmail" size=50 required> <br>
        D.O.B    : <input type=date name="txtDOB" size=20 required> <br>
        Age      : <input type=text name="txtAge" size=5 maxlength=2 required> 
        <br><br>
        Username         : <input type=text name="txtUserName" size=20 
            required> <br>
        Password         : <input type=password name="txtPwd" size=20 
            required> <br>
        Re-Enter Password: <input type=password name="txtPwd2" size=20 
            required> <br><br>
        <input type=submit name=btnSubmit value=SAVE> 
    </form>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

您需要在最后添加返回到您的java脚本函数

function fnValidate()
{
    with document.frmAdd{

    var x;
    x = txtEmail.value;

    if( x.indexof("@")== -1)
    {   alert("A valid email address must contains @"); 
        return false; }
    if( isNaN(txtAge.value)== 1- )
    {   alert("Age must be filled out by number");
        return false; }
    if( txtPwd.value <> txtPwd2.value )
    {   alert("Password not matched!");
        return false; }

    } 

return false;
} 

答案 1 :(得分:0)

Javascript函数应放在脚本标记内:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript">
    function fnValidate()
    {
        with document.frmAdd{

        var x;
        x = txtEmail.value;

        if( x.indexof("@")== -1)
        {   alert("A valid email address must contains @"); 
            return false; }
        if( isNaN(txtAge.value)== 1- )
        {   alert("Age must be filled out by number");
            return false; }
        if( txtPwd.value <> txtPwd2.value )
        {   alert("Password not matched!");
            return false; }

        } //end_with
    } //end_fnValidate()

    </script>
    </head>
    <body>
    .
    .
    .
    </body>
    </html>

答案 2 :(得分:0)

用圆括号括起with语句表达式。话虽如此,不建议再使用with语句(参见MDN documentation)。

function fnValidate()
{
  with (document.frmAdd) {
    var x;
    x = txtEmail.value;
    if( x.indexof("@")== -1)
    {   alert("A valid email address must contains @"); 
        return false; }
    if( isNaN(txtAge.value)== 1- )
    {   alert("Age must be filled out by number");
        return false; }
    if( txtPwd.value <> txtPwd2.value )
    {   alert("Password not matched!");
    return false; }
  } //end_with
  return;
} //end_fnValidate()