Jquery validate 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());
}
EDIT :
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:'"开发者_JAVA技巧 + $('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.
精彩评论