当前位置:首页 > Web开发 > 正文

//必填 不能为空 自带的校验方法 holderDateValidity:true //自定义规则 }

2024-03-31 Web开发

简单记录一下  jquery-validator 的使用要领

首先我们要引入jquery和jquery-validator

给张图看一下 校验的提示要领

技术图片

引入之后开始使用

// 自界说校验要领 返回 true 和 false $.validator.addMethod( "holderDateValidity", //自界说的 校验要领名称 function (value, element) { //value 使用当前校验要领的 dom元素的value 值,,element 使用当前校验要领的 dom元素 var effectiveDate4 = $(‘#effectiveDate4‘).val(); var effectiveDate5 = $(‘#effectiveDate5‘).val(); if (effectiveDate4 == ‘‘ && effectiveDate5 == ‘‘){ //本身界说校验信息 return false; }else{ return true; } } );

然后进行使用

<form id="basicInfoForm">

    <div class="input-item">

          <label class="input-name">证件有效日期<span class="must">*</span></label>

          <input id="effectiveDate4" name="effectiveDate4" class="Wdate pa_ui_element_normal input-text" autocomplete="off"

           style="width: 216px;">

          <input id="effectiveDate5" name="effectiveDate5" class="Wdate pa_ui_element_normal input-text" autocomplete="off"

           style="width: 216px;">

      </div>

</form>

//提交时校验表单信息

$("#basicInfoForm").validate({

     rules: {  //界说法则

           effectiveDate4: {  //表单提交的 name 属性为 effectiveDate4的dom元素

              required: true,   //必填 不能为空  自带的校验要领

              holderDateValidity: true  //自界说法则

        },

     messages: {

      effectiveDate4: {

              required: "投保险人证件有效日期不能为空",   //required为空的时候 提示语

              holderDateValidity: "投保险人证件有效日期不能为空"  //自界说法则的提示语

          },

     }

})

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/32656.html