$(document).ready(function(){
$('#submitAndConfirm').on('click', function() {
alert("Hi");
$('#addPat').validate({
rules:{
iscosId:{required:true},
pat_id:{required:true},
admissionDate:{required:true},
dischargeDate:{required:true},
},messages:{
iscosId:"Please reload the Page",
pat_id:"This field cannot be empty",
admissionDate:"This field cannot be empty"
}
});
});
});
从上面的代码中,我必须在单击按钮后验证表单,但是它不起作用,有人可以帮我吗?
最佳答案
您可以尝试这种方法吗?您可以检查输入字段是否为空,并且还捕获空格。将类添加到所有输入字段,例如class =“ required”,然后为每个输入字段添加属性字段名和相应的值。
var requiredFields = "";
$("#myForm").find('.required').each(function () {
if ($(this).val().trim().length == 0) {
requiredFields += " - " + $(this).attr("fieldname") + "\n";
}
});
if (requiredFields != "") {
alert("Please enter the following required field(s): \n" + requiredFields);
} else {
//save here
}
关于javascript - 单击按钮Jquery验证表单,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33781394/