如何用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
相关产品推荐
相关产品推荐

