我的自定义验证方法有什么问题?

时间:2013-08-12 13:59:29

标签: php jquery jquery-validate

我正在使用jquery插件的addmethod,但我想我会想念一些东西,因为当我尝试它时,它没有验证任何东西,只是传递提交表单。

我想做的是,禁用在表单中使用的几个单词。

这是完整的脚本:

$().ready(function () {
    // validate the comment form when it is submitted
    $("#commentForm").validate();

    // validate signup form on keyup and submit
    $(".validate").validate({
        submitHandler: function (form) {
            if ($("#btn_register").length > 0) {
                $.ajax({
                    url: "register.php?action=email_avail&email=" + $('#email').val(),
                }).done(function (res) {
                    if (res == 0) {
                        alert('Email address is already registered');
                    } else if (res == 1) {
                        alert('Username is already registered');
                    } else if (res == 2) {
                        form.submit();
                    }
                });
                return false;
            }
            form.submit();
        },
        rules: {
            firstname: {
                required: true,
                words: true,
            },
            lastname: "required",
            username: {
                required: true,
            },
            password: {
                required: true,
            },
            c_password: {
                required: true,
                equalTo: "#password"
            },
            email: {
                required: true,
                email: true,
            },
            topic: {
                required: "#newsletter:checked",
                minlength: 2
            },
            address2: {
                required: true,
                number: true,
                minlength: 13,
                maxlength: 13
            },
            agree: "required"
        },
        messages: {
            //firstname: "Please enter your firstname",
            //lastname: "Please enter your lastname",
            username: {
                //  required: "Please enter a username",
                //  minlength: "Your username must consist of at least 2 characters"
            },
            password: {
                //  required: "Please provide a password",
                //  minlength: "Your password must be at least 5 characters long"
            },
            confirm_password: {
                //  required: "Please provide a password",
                //  minlength: "Your password must be at least 5 characters long",
                equalTo: "Please enter the same password as above"
            },
            email: "Molim Vas upišite ispravnu email adresu.",
            //agree: "Please accept our policy"
        }
    });
});


$.validator.addMethod("words", function (value, element) {
    return /^/ ( ? !Hello)( ? !World) $ / .test(value);
}, "You are not allowed to use words such as Hello or World");

这是html:

<form action="" method="post" name="frm_register" id="frm_register" class="validate">
<table cellpadding="5" cellspacing="0" width="100%" border="0" align="center">
<tr align="left" >
<td width="150">Email adresa</td>
<td>: <input type="text" name="email" id="email" class="required form_white"></div>        </td>
</tr>

<tr align="left" >
<td>Lozinka</td>
<td>: <input type="Password" name="password" id="password" class="required form_white">    </td>
</tr>

<tr align="left" >
<td>Potvrda lozinke</td>
<td>: <input type="Password" name="c_password" class="form_white"></td>
</tr>

<tr align="left" >
<td width="150">Ime</td>
<td>: <input type="text" name="firstname" class="required form_white"></td>
</tr>

<tr align="left" >
<td width="150">Prezime</td>
<td>: <input type="text" name="lastname" class="required form_white"></td>
</tr>

<tr align="left" >
<td width="150">JMBG</td>
<td>: <input type="text" name="address2" class="required form_white"></td>
</tr>

<tr align="left" >
<td width="150">Mesto</td>
<td>: <input type="text" name="zipcode" class="required form_white"></td>
</tr>


<tr align="left" >
<td> </td>
<td><input type="hidden" name="btn_register" id="btn_register" value="Login">&nbsp;
<div class="registrujse"><INPUT type="submit" name="btn_submit" id="btn_submit"     value=""></div></td>
</tr>

</table>

1 个答案:

答案 0 :(得分:0)

看起来您的正则表达式可能无效。无论哪种方式,我都会组合一个快速的jsfiddle来验证名字,并且在Hello或World不包含之前不允许提交(顺便提一下这些是区分大小写的):

http://jsfiddle.net/C7Mt4/13/

这是“单词”的验证码:

$.validator.addMethod("words", function (value, element) {
return /^((?!Hello)(?!World).)*$/.test(value);