我想在验证函数之外编辑错误消息。所以我有一个简单的密码恢复表格。这是html:
<div style="position:relative; left:40%;" class="pass_recovery">
<h2 class="title">Recover</h2>
<form action="" method="POST" id="recover_pass">
<span style="font-size:12px;">Email you registered with: </span><input type="text" name="recov_pass" id="recov_pass"<br>
<input type="submit" value="Send Password">
</form><br>
</div>
这是我用于验证的 jquery:
var pass_rec = $('#recover_pass').validate({
onkeyup: false,
errorClass: "password_messages",
rules: {
recov_pass: {
required: true,
email: true
}
},
messages: {
recov_pass: {
required: "Please enter an email address",
email: "Please enter a valid email address"
}
}
});
然后是ajax调用。这只是检查输入的电子邮件是否是有效的注册电子邮件地址。
$('#recover_pass').submit(function() {
if(pass_rec.form()) {
var $this = $(this);
$.ajax({
data: $this.serialize(), // get the form data
type: "POST", // GET or POST
url: "Private/Recover_pass.php", // the file to call
success: function(data) { // on success..
if(data=="pass") {
$('.password_messages').text('good');
}
else {
$('.password_messages').text('bad');
}
},
error: function(data) {
$('.password_messages').text('error');
},
complete: function(data) {
$('#recov_pass').val("");
}
});
}
return false; //so it doesn't refresh when submitting the page
});
这不起作用,因为当我输入未注册的虚拟电子邮件时,例如example@ex.com
,它不会显示该bad
消息。如何在验证功能之外显示错误消息?