jquery使用多个if / else条件验证规则

时间:2016-08-02 03:25:51

标签: javascript jquery ajax jsp jquery-validate

我想将jquery validate()规则用于2个交叉字段。如果在另一个字段中键入任一字段,则还需要。此外,一旦需要它们,数字字段的格式应该是第1个15位数应该是整数,日期字段应该是mm / dd / yyyy格式,日期应该小于今天的日期。

//..

    $("#adjustmentsFormID").validate({
    rules: {            
    refTranNbr: "required",
    refTranDate: "required"
    },
        messages: {         
            refTranNbr: {
                required: function (element) {
                    if($("#refTranDate").val().length > 0){
                        return "Please enter the reference transaction number ";
                    } else if(!refNumChk($("#refTranNbr").val())){
                         return "Please enter a valid Reference Transaction Number";                    
                    } else {
                        return false;
                    }
                }
            },          
            refTranDate: {
                required : function (element) {
                    var tdate = $("#refTranDate").val();
                    if($("#refTranNbr").val().length > 0){                      
                        return "Please enter a date for the Refering Transaction to complete this transaction.";
                    }else if((new Date() > new Date(tdate))) {
                        return "Please enter a reference transaction date less than today's date.";
                    }else{
                        return false;
                    }

                }
        }, 
    }); 


..//

在这两种情况下,必填字段的第一个条件都有效。但是对于refNum字段,具有refNumChk的第二个条件不起作用。实际上它没有被调用。类似地,对于refTranDate,所需的字段验证工作但是日期> tDate未被检查。不确定这种方法是否有效,或者我应该为多种情况做些不同的事情。

1 个答案:

答案 0 :(得分:1)

您的jQuery验证方法是错误的,messages用于在验证错误的情况下仅返回错误消息。

因此,您所做的唯一验证是必需的验证,您可以添加自定义验证规则来解决此问题

jQuery(function($) {
  jQuery.validator.addMethod("refNumChk", function(value, element, params) {
    return this.optional(element) || /^\d{15}[A-Z]$/.test(value);
  }, jQuery.validator.format("Enter a value in forat aa-999"));
  jQuery.validator.addMethod("lessThanToday", function(value, element, params) {
    return this.optional(element) || new Date() > new Date(value);
  }, jQuery.validator.format("Value should be less than today"));


  $("#adjustmentsFormID").validate({
    rules: {
      refTranNbr: {
        required: true,
        //refNumChk: true
        pattern: /^\d{15}[A-Z]$/
      },
      refTranDate: {
        required: true,
        lessThanToday: true
      }
    },
    messages: {
      refTranNbr: {
        required: "Please enter the reference transaction number",
        pattern: "Please enter a valid Reference Transaction Number"

      },
      refTranDate: {
        required: "Please enter a date for the Refering Transaction to complete this transaction.",
        lessThanToday: "Please enter a reference transaction date less than today's date."
      },
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/additional-methods.js"></script>
<form id="adjustmentsFormID" method="post" action="">
  <div>
    <input name="refTranNbr" />
  </div>
  <div>
    <input name="refTranDate" />
  </div>

  <input type="submit" value="Save" />
</form>