单击按钮Jquery验证表单

时间:2015-11-18 13:30:05

标签: javascript jquery

$(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"
             }
        });
     });
 });

从上面的代码我必须在单击按钮后验证表单但它不起作用,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

你可以尝试这种方法。你可以检查输入字段是否为空并且还会捕获空格/秒。将类添加到所有输入字段,例如class =“required”并为每个输入字段添加具有相应值的属性fieldname。

 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
    }