You need to enable JavaScript to run this app. 如何使用jQuery Validation实现年/月输入字段的择一校验
优惠活动
大模型
产品
解决方案
定价
更多

如何使用jQuery Validation实现年/月输入字段的择一校验

分步表单「月度收入领取时长」模块校验调整

规则说明

  • 模块功能:采集用户领取某类月度收入的持续时长,拆分为「年」「月」两个独立输入项
  • 现存异常:用户只要在两个输入框中输入任意内容,当前表单步骤就会自动向后跳转,未对输入内容的合规性做校验
  • 预期通过条件:年输入框输入合规数值,或是月输入框输入合规数值,两个条件满足任意一个,即可通过当前步骤校验,进入后续流程

现有页面结构

<fieldset class="steps step7 au_cr_feild" name="stp_receive_period">
    <h2 class="au-cr-title">How Long Have You Been <br>Receiving This Monthly Income?</h2>
    <div class="line-divider"></div>
    <div class="input-label">
        <input type="tel" placeholder="0" name="Years_At_Employer" class="valid" aria-invalid="false">
        <span>Years</span>
    </div>
    <div class="input-label">
        <input type="tel" placeholder="0" name="Months_At_Employer" aria-required="true"
            class="valid" aria-describedby="Months_At_Employer-error" aria-invalid="false">
        <span>Months</span>
    </div>
</fieldset>

现有校验逻辑

基于jQuery Validation的原有配置如下:

if (stp_name == "stp_receive_period") {
  $.validator.addMethod("Years_At_Employer", function(value, element) {
     if (value >= 1) {
         return true;
     }
  }, "Minimum 1 Years to apply");

  // 校验规则配置
  settings = {
     ignore: [],
     rules: {
         "Years_At_Employer": {
             required: true,
             number: true,
             Years_At_Employer: true
         },
         "Months_At_Employer": {
             number: true,
         }
     },
     messages: {
         "Years_At_Employer": {
             required: "Please input number of years",
             number: "Number of years should be a number value"
         },
         "Months_At_Employer": {
             number: "Number of months should be a number value"
         }
     },
     errorElement: 'span',
     errorPlacement: function(error, element) {
         error.appendTo($("[name=" + stp_name + "]").find(".error_msg"));
     }
  };
}

调整实现

  1. 修复原有自定义校验方法的逻辑漏洞
    原Years_At_Employer自定义方法仅在值≥1时返回true,未在不满足条件的场景显式返回false,会被jQuery Validation误判为校验通过,是输入任意内容就触发步骤跳转的核心诱因。
  2. 实现「年/月择一合规即通过」的校验规则
    • 移除原规则中对年输入框的强制required配置,不再要求用户必须填写年数值
    • 新增自定义聚合校验方法,统一判断两个输入框的合规性:
      • 合规年数值定义:输入为有效数字,数值≥1
      • 合规月数值定义:输入为有效数字,数值在0-12的合法区间内
      • 两个输入框满足任意一项的合规要求,整体校验即通过;否则抛出统一提示
  3. 调整后的完整校验代码:
if (stp_name == "stp_receive_period") {
  // 自定义聚合校验:年/月任意一个输入合规即通过
  $.validator.addMethod("durationCheck", function(value, element) {
    const yearsInput = $("[name='Years_At_Employer']").val().trim();
    const monthsInput = $("[name='Months_At_Employer']").val().trim();
    const yearsVal = Number(yearsInput);
    const monthsVal = Number(monthsInput);

    // 年合规判断:非空、有效数字、值≥1
    const yearsValid = yearsInput !== "" && !isNaN(yearsVal) && yearsVal >= 1;
    // 月合规判断:非空、有效数字、值在0-12区间
    const monthsValid = monthsInput !== "" && !isNaN(monthsVal) && monthsVal >= 0 && monthsVal <= 12;

    return yearsValid || monthsValid;
  }, "请输入合规的收入领取持续时长");

  settings = {
    ignore: [],
    rules: {
      "Years_At_Employer": {
        number: true,
        durationCheck: true
      },
      "Months_At_Employer": {
        number: true,
        durationCheck: true
      }
    },
    messages: {
      "Years_At_Employer": {
        number: "Number of years should be a number value"
      },
      "Months_At_Employer": {
        number: "Number of months should be a number value"
      }
    },
    errorElement: 'span',
    errorPlacement: function(error, element) {
        error.appendTo($("[name=" + stp_name + "]").find(".error_msg"));
    }
  };
}

内容的提问来源于stack exchange,提问作者Abhi Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.08 11:27:04