文檔:https://jqueryvalidation.org/rules/#rules-%22remove%22-rules
rules( "add", rules )
"add"
Type: String
rules( "remove", rules )
"remove"
Type: Stringjavascript
Example: Adds minlength: 2 to an element which is already required.
$( "#myinput" ).rules( "add", {
minlength: 2
});
Example: Adds required and minlength: 2 to an element and specifies custom messages for both.
$( "#myinput" ).rules( "add", {
required: true,
minlength: 2,
messages: {
required: "Required input",
minlength: jQuery.validator.format("Please, at least {0} characters are necessary")
}
});
Example: Removes all static rules from an element.
$( "#myinput" ).rules( "remove" );
$( "#myinput" ).rules( "remove", "min max" );java
根據【發票類型】的選值,動態設置【價稅合計】、【校驗碼】驗證規則jquery
<script type="text/javascript"> var prefix = ctx + "invoice/invIncomeInvoice" $("#form-invIncomeInvoice-add").validate({ debug: true, rules : { invoiceType : { required : true, check_invoiceType : true }, invoiceCode : { required : true, check_invoiceCode : true }, invoiceNo : { required : true, check_invoiceNo : true }, invoiceDate : { required : true, dateISO : true } } }); var errorClass = 'error'; /**爲字段追加驗證規則*/ $.validator.addMethod("check_invoiceType",function(value,element,params){ if(value=='page_special_invoice'){ $("#totalTax").rules( "add", { required : true, check_totalTax : true }); $("#antiFakeCode").rules( "remove" ); //$("#antiFakeCode").rules( "remove", "min max" ); //$("#antiFakeCode-error").text("").removeClass("err icon-reg-err").addClass("scc icon-reg-scc"); //$("#antiFakeCode").removeClass(errorClass); //$("#antiFakeCode").parents('div:first').children('label').removeClass(errorClass); $("#antiFakeCode").removeClass(errorClass); $("#antiFakeCode-error").remove(); } else { $("#totalTax").rules( "remove" ); $("#antiFakeCode").rules( "add", { required : true, maxlength : 20 }); //$("#totalTax-error").text("").removeClass(errorClass).addClass("scc icon-reg-scc"); //$("#totalTax").removeClass(errorClass); //$("#totalTax").parents('div:first').children('label').removeClass(errorClass); $("#totalTax").removeClass(errorClass); $("#totalTax-error").remove(); } return true; },""); $.validator.addMethod("check_invoiceCode",function(value,element,params){ var check = /\d{6,15}$/; return this.optional(element)||(check.test(value)); },"*只容許8-15位數字!"); $.validator.addMethod("check_invoiceNo",function(value,element,params){ var check = /\d{6,15}$/; return this.optional(element)||(check.test(value)); },"*只容許8-15位數字!"); $.validator.addMethod("check_totalTax",function(value,element,params){ var check = /^\d+\.?\d{0,2}$/; var invoiceType = $("#invoiceType").val(); if(invoiceType=='page_special_invoice'){ return this.optional(element)||(check.test(value)); } return true; },"*保留兩位小數的金額!"); function submitHandler() { if ($.validate.form()) { $.operate.save(prefix + "/add", $('#form-invIncomeInvoice-add').serialize()); } } </script>