在通过javascript验证后,表单不会重定向

时间:2013-03-25 02:33:27

标签: jquery validation registration

我使用代码验证我的注册表单:http://rickharrison.github.com/validate.js/ 这是我自定义的代码:

<!DOCTYPE html>
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

     <link href="./js/main.css" rel="stylesheet" type="text/css">

    <script type="text/javascript" async="" src="./js/ga.js"></script>
    <script type="text/javascript" src="./js/jquery.min.js"></script>
    <script type="text/javascript" src="./js/validate.min.js"></script>
</head>
<body>


    <form name="createAccount" action="validate.php" method="POST">
        <label for="fname">First name:</label>
        <input name="fname" id="fname">

        <label for="lname">Last name:</label>
        <input name="lname" id="lname">
         <br/>
        <label for="email">Email:</label>
        <input name="email" id="email">
         <br/>
        <label for="password">Password:</label>   
        <input name="password" id="password" type="password">
         <br/>
        <label for="vPassword">Re-type password:</label> 
        <input name="vPassword" id="vPassword" type="password">
         <br/>
        <button class="button gray" type="submit" name="submit">Submit</button>
    </form>
    <div class="success_box">Si jvois ca ben tbnk</div>
    <div class="error_box"></div>
<script type="text/javascript">

    new FormValidator('createAccount', [{
        name: 'fname', 
        display: 'Please enter your first name.',
        rules: 'required'
    }, {
        name: 'lname',
        display: 'Please enter your name.',
        rules: 'required'
    }, {
        name: 'email',
        display: 'Please enter a valid email address.',
        rules: 'valid_email'
    }, {
        name: 'password',
        display: 'Please enter a password of at least 8 characters.',
        rules: 'required|min_length[8]'
    }, {
        name: 'vPassword',
        display: 'The passwords do not match.',
        rules: 'required|matches[password]'
    },  ], function(errors, event) {
        var SELECTOR_ERRORS = $('.error_box'),
            SELECTOR_SUCCESS = $('.success_box');
        if (errors.length > 0) {
            SELECTOR_ERRORS.empty();

            for (var i = 0, errorLength = errors.length; i < errorLength; i++) {
                SELECTOR_ERRORS.append(errors[i].message + '<br />');
            }

            SELECTOR_SUCCESS.css({ display: 'none' });
            SELECTOR_ERRORS.fadeIn(200);
        } else {
            SELECTOR_ERRORS.css({ display: 'none' });
            SELECTOR_SUCCESS.fadeIn(200);
        }

        if (event && event.preventDefault) {
            event.preventDefault();
        } else if (event) {
            event.returnValue = false;
        }
    });

    </script>


</body>
</html>

我尝试将“success_box”更改为函数以重定向到我的validate.php页面,但没有任何效果。我尝试将所有内容删除到success_box,如果错误数为0

,则创建一个重定向函数
if (errors === 0){ window.location = "http://www.google.com/";}   

但这也不起作用。我没有答案......

谢谢!

2 个答案:

答案 0 :(得分:0)

if (errors == 0){ window.location = "http://www.google.com/";}

你不能在条件中使用单=

答案 1 :(得分:0)

Look it's working for real!!

我刚刚在验证器规则数组中的最后一个对象之后删除了逗号}, ], function(errors, event) {

好的,我明白了。如果您希望表单在数据有效时充当常规表单,则不要为该情况调用preventDefault()(因为您需要默认的表单行为)。