以下是我的JSP页面,
<input name="txtemailid" type="text" id="txtemailid" placeholder="Email Id">
<input name="txtusername" type="text" id="txtusername" placeholder="User Name">
<input name="txtpassword" type="password" id="txtpassword" placeholder="Password">
<input name="txtcpassword" type="password" id="txtcpassword" placeholder="Confirm Password">
我的jquery验证脚本如下,
$(document).ready(
function() {
$('#frmCustomer').validate({
rules : {
txtemailid : {
required : true,
email : true
},
txtusername : {
required : true
},
txtpassword : {
required : true
},
txtcpassword : {
required : true,
equalTo : "#txtpassword"
}
},
messages : {
txtemailid : {
required : 'Please Enter Email Id.',
email : 'Please Enter valid Email Id.'
},
txtusername : {
required : 'Please Enter User Name.'
},
txtpassword : {
required : 'Please Enter Password.'
},
txtcpassword : {
required : 'Please Enter Confirm Password.',
equalTo : 'Passwords do not match.'
}
},
errorPlacement : function(label, element) {
$('<span class="arrow"></span>').insertBefore(element);
$('<span class="error"></span>').insertBefore(element).append(label);
},
});
});
问题是,在用户在字段中输入任何内容之前,会显示错误消息Invalid email
和password does not match
。我在弹出框中打开JSP页面。请帮助。
提前谢谢。
答案 0 :(得分:3)
您已在doc ready上直接验证,而您可以使用submit and keydown
方法:
$(document).ready(function() {
$('#frmCustomer').on('submit keydown', function(){
$(this).validate({
rules : {
........
},
messages : {
........
},
errorPlacement : function(label, element) {
..........
} //<----here you have a ',' which is not acceptable in ie
});
});
});
您可以使用.on()
方法在元素上应用多个事件。