You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用jQuery Validator验证两种格式的身份证号码?

Solution for Dual Format Validation

Got it! To support both valid formats you mentioned, you just need to expand your regular expression with a logical OR (|) to include the 12-digit number case. Here's the adjusted validation method:

jQuery.validator.addMethod("checkformat", function(value, element) {
  // Regex matches either 9 digits + v/V/x/X OR 12 digits exactly
  return /^(?:[0-9]{9}[vVxX]|[0-9]{12})$/.test(value);
}, 'Invalid National Identity Card Number');

Breakdown of the updated regex:

  • ^ and $: Ensure the entire input string matches the pattern (no extra characters before/after)
  • (?:...): A non-capturing group to group the two valid format options together without creating unnecessary capture groups
  • [0-9]{9}[vVxX]: Your original valid format (9 digits followed by a case-insensitive v or x)
  • |: Acts as an "OR" operator to separate the two valid patterns
  • [0-9]{12}: The new valid format (exactly 12 digits)

If you prefer a slightly shorter regex, you can replace [0-9] with \d (note: \d matches all Unicode digits in some regex engines, so stick with [0-9] if you only want to allow Arabic numerals):

return /^(?:\d{9}[vVxX]|\d{12})$/.test(value);

内容的提问来源于stack exchange,提问作者Thilina Sameera

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:34:18