JSP jquery验证规则,带有多个if/else条件

qvtsj1bj  于 2023-06-27  发布在  jQuery
关注(0)|答案(1)|浏览(148)

我想使用jQuery validate()规则来2个交叉字段。如果在其中一个字段中键入了另一个字段,则也需要输入。此外,一旦他们需要有一个数字字段的格式应该是第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所需的字段验证也可以工作,但date > tDate未被选中。不确定这个方法是否有效,或者我应该为多个条件做一些不同的事情。

l7wslrjt

l7wslrjt1#

您的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>

相关问题