私は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">
<div class="registrujse"><INPUT type="submit" name="btn_submit" id="btn_submit" value=""></div></td>
</tr>
</table>