1

我正在对 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>
4

1 回答 1

0

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

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

这是我对“单词”的验证码:

$.validator.addMethod("words", function (value, element) {
return /^((?!Hello)(?!World).)*$/.test(value);
于 2013-08-12T14:57:44.837 回答