我正在使用jquery插件来验证我的表单。如果有不符合验证的条件,我想阻止用户提交表单。这是我在验证中使用的一段代码
$(function() {
$("#register-form").validate({
rules: {
OMNI_FIRSTNAME: {
required: true,
minlength: 2,
maxlength: 50
},
OMNI_LASTNAME: {
required: true,
minlength: 2,
maxlength: 50
},
messages: {
OMNI_FIRSTNAME: "Please enter first name",
OMNI_FIRSTNAME: {
required: "Please provide firstname",
minlength: "Firstname must be at least 2 character long",
maxlength: "Firstname reached maximum length of 50 characters long"
},
OMNI_LASTNAME: "Please enter last name",
OMNI_LASTNAME: {
required: "Please provide lastname",
minlength: "Lastname must be at least 2 character long",
maxlength: "Lastname reached maximum length of 50 characters long"
},
submitHandler: function(form) {
form.submit();
}
});
});
只要不满足条件,上面的代码就会向我的表单输出消息。我的表单$('#register-form')
中有一个提交按钮,用于发布从输入字段(OMNI_FIRSTNAME
,OMNI_LASTNAME
)输入的数据。我如何将这两个流程整合为一个流程?目前,我仍然可以发布不符合我的jquery验证的数据。
答案 0 :(得分:2)
在jquery代码中进行以下更改
submitHandler: function(form) {
form.preventDefault();
form.submit();
}