我希望在验证期间显示警报,但没有解决此问题,
我的代码是:
$(document).ready(function(){
$('#myform').validate({
rules: {
name: {
required: true,
digits: false
},
email: {
required: true,
email: true
},
pswd: {
required: true,
minlength: 5
},
cpswd: {
required: true,
equalTo: "#pswd"
},
agree: "required"
},
messages: {
name: {
required: "Enter Valid Name",
digits: "Name contains characters only"
},
email: {
required: "Enter Email",
email: "Enter Valid Email (i.e. someone@example.com)"
},
pswd: {
required: "Enter Valid Password",
minlength: "Maximum allowed length is 5 Characters"
},
cpswd: "Password not Matched"
},
submitHandler: function(form) {
var name = $("#name").val();
var email = $("#email").val();
var pswd = $("#pswd").val();
$("#submit").click(function() {
alert("Data Submitted Successfully!\nName : "+ name +"\nEmail : "+ email +"\nPassword : "+ pswd);
form.submit();
return false;
});
}
});
});
此代码将错误显示为文本消息。
我想在警告框中显示错误。
在消息中我发出如下警告:
messages: {
name: alert('Enter Name')
},
然后它会在页面加载时显示所有警报。
任何想法怎么做?
答案 0 :(得分:1)
您可以使用errorPlacement:
选项。
First argument
:创建的错误标签为jQuery对象。
Second argument
:作为jQuery对象的无效元素。
errorPlacement: function (error, element) {
alert(error.text());
}
注意:某些浏览器会陷入无限循环。
答案 1 :(得分:0)
你可以这样做:
$(document).ready(function () {
$('#myform').validate({
onclick: false,//add this to your code
rules: {
name: "required"
},
messages: {
name: {
required: "The Name is required!"
}
},
errorPlacement: function (error, element) {
alert(error.text());
},
submitHandler: function (form) { // for demo
alert('valid form submitted'); // for demo
return false; // for demo
}
});
});