Я использую вызов метода для события OnValid, но значения в моей форме очищаются к тому времени, когда я пытаюсь ссылаться на них в моем методе. Кто-нибудь знает, что я делаю не так?
$("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}». Вот мой исправленный код:
$.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");
}
}
});