jQuery验证onValid [英] Jquery validate onValid
问题描述
我正在使用OnValid事件上的方法,但是当我尝试在我的方法中引用它们时,表单中的值已被清除.有人知道我在做什么错吗?
I am using calling a method on the OnValid event, but the values in my form have cleared by the time I try to reference them in my method. Does anybody know what I am doing wrong?
$("form").validate({
//errorLabelContainer: $("#divErrors"),
rules: {
txtUserName: {
required: true,
minlength: 4,
maxlength: 20
},
txtPassword: {
required: true,
minlength: 4,
maxlength: 32
},
txtConfirmPassword: {
required: true,
equalTo: "#txtPassword",
minlength: 4,
maxlength: 32
},
txtFirstName: {
required: true,
maxlength: 50
},
txtLastName: {
required: true,
maxlength: 50
},
txtJobTitle: {
required: true,
maxlength: 100
},
txtEmailAddress: {
required: true,
email: true,
maxlength: 100
},
txtTelephoneNumber: {
required: true,
number: true//,
//postalCode:true
}
},
messages: {
txtUserName: {
required: "Please enter a User Name",
minlength: "User Name must be at least 4 characters",
maxlength: "User Name must be no more than 20 characters"
},
txtPassword: {
required: "Please enter a Password",
minlength: "Password must be at least 4 characters",
maxlength: "Password must be no more than 32 characters"
},
txtConfirmPassword: {
required: "Please confirm Password",
equalTo: "Confirm Password must match Password",
minlength: "Confirm Password must be at least 4 characters",
maxlength: "Confirm Password must be no more than 32 characters"
},
txtFirstName: {
required: "Please enter a First Name",
maxlength: "First Name must be no more than 50 characters"
},
txtLastName: {
required: "Please enter a Last Name",
maxlength: "Last Name must be no more than 50 characters"
},
txtJobTitle: {
required: "Please enter a Job Title",
maxlength: "Job Title must be no more than 100 characters"
},
txtEmailAddress: {
required: "Please enter an Email Address",
email: "Please enter a valid Email Address",
maxlength: "Email Address must be no more than 100 characters"
},
txtTelephoneNumber: {
required: "Please enter a Telephone Number",
number: "Telephone Number must be numeric"
}
}
onValid : function() {
addUser();
}
});
});
function addUser() {
alert($('input[name="txtUserName"]').val());
}
非常感谢您的帮助!当您为SubmitHandler指定一个函数时,是否可以使用它来调用一个方法,例如进行附加验证,然后再调用另一个方法以写入数据库?我在SubmitHandler中指定了一个方法,但收到错误无效标签{"d":0}.这是我修改后的代码:
Many thanks for your help! When you specify a function for submitHandler, can you use it to call a method to for example do additional validation and then calling another method to write to the database? I have specified a method in my submitHandler, but I get the error 'invalid label {"d":0}'. Here is my amended code :
$.validator.setDefaults({
submitHandler: function() { addUser(); }
});
function addUser() {
//check for unique username and email
$.ajax(
{
type: "POST",
url: "/Services/CDServices.asmx/CheckForUniqueUserName",
data: "{strUserName:'" + $('input[name="txtUserName"]').val() + "'}",
async: false,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(msg) {
if (msg.d == 0) {
alert("already exists");
}
else {
alert("username is unique");
}
}
});
推荐答案
$.validator.setDefaults({
submitHandler: function() { alert("submitted!"); }
});
$().ready(function() {
// validate the comment form when it is submitted
$("#commentForm").validate();
})
来自 验证 的来源.
From the source of Validate.
这篇关于jQuery验证onValid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!