我正面临一个问题,以验证表单onclick事件。
我正在使用一个表单,但它不包含任何按钮或提交事件。而不是一个按钮,我使用<a>
,所以我怎么能管理.valide()
当用户点击一个<a> </a>
?
这是我的代码的表单链接,是由用户点击提交表单:
<a style="cursor: pointer" class="edit_submit_btn"><?php echo "Update"; ?></a>
jquery代码(运行正常):
$(document).on('click','.edit_submit_btn',function(){
)};
但是当我试图使用.validate
验证表单时,它不起作用。
验证代码:
$('.user_edit').validate({
onkeyup: false,
errorClass: 'has-error-text',
validClass: 'valid',
rules: {
"data[PersonMaster][first_name]": {
required: true
},
"data[PersonMaster][last_name]": {
required: true
},
"data[PersonMaster][email_address]": {
required: true,
email: true
},
"data[PersonMaster][mobile_number_code]": {
required: true
},
"data[PersonMaster][mobile_number]": {
required: true
},
"data[PersonMaster][country_id]": {
required: true
}
},
messages: {
"data[PersonMaster][first_name]": {
required: globalValidations.Modules.addUser.first_name
},
"data[PersonMaster][last_name]": {
required: globalValidations.Modules.addUser.last_name
},
"data[PersonMaster][email_address]": {
required: globalValidations.Modules.addUser.email_address,
email:globalValidations.Modules.addUser.email_valid
},
"data[PersonMaster][mobile_number_code]": {
required: globalValidations.Modules.addUser.mobile_number_code
},
"data[PersonMaster][mobile_number]": {
required: globalValidations.Modules.addUser.mobile_number
},
"data[PersonMaster][country_id]": {
required: globalValidations.Modules.addUser.country_code
}
},
highlight: function(element) {
alert("hi");
$(element).closest('div').addClass("has-error");
},
unhighlight: function(element) {
$(element).closest('div').removeClass("has-error");
},
errorPlacement: function(error, element) {
$(element).closest('div').append(error);
},
submitHandler: function(form) {
$("#loading_main").show();
var data = $(form).serialize();
var page_number = $("#page_value").val();
$.ajax({
url: site_url + "user_masters/edit_action",
data: data,
type: "POST",
success: function(response) {
$("#loading_main").hide();
var response_object = jQuery.parseJSON(response);
if (response_object.error_code == 0) {
UserloadPiece(site_url + "user_masters/user_list/" + (new Date()).getTime() + '/page:' + page_number + '', ".user-content-table-inner", '');
// setTimeout(message_hide("alert", "", "alert-success"), 5000);
} else if (response_object.error_code == '1') {
$("#alert-edit-profile").show();
$("#alert-edit-profile").removeClass("alert-success");
$("#alert-edit-profile").addClass("alert-danger");
$("#alert-edit-profile").html(response_object.message);
} else if (response_object.error_code == '2') {
var i = 0;
$.each(response_object.message, function(field, error) {
if (field != "null") {
$(".has-error-text").empty();
$("#" + field + "_validation").closest('div').addClass("has-error");
$("#" + field + "_validation").text(error);
$("#" + field + "_validation").show();
if (i == 0)
$("#" + field).focus();
i = i + 1;
}
});
}
else if(response_object.error_code == '9'){
handle_redirect();
}
}
});
},
error: function(jqXHR, textStatus, errorThrown) {
alert("There is some error while processing your request. please try again later.");
window.location.href = site_url + 'user_masters/login';
}
});
只需使用表单id来验证表单
$('#form_id').validate();
,并在输入字段中添加属性
data-rule-required=true
如果你想自定义消息,只需添加另一个属性
data-msg-required="Your message"