卡片的格式是什么?Regex

What is the format for the card? Regex

本文关键字:Regex 是什么 格式      更新时间:2023-09-26

我想验证插入卡号的输入

我尝试了这种格式,但字段被验证,如果少于16个数字。

我如何更改常数以不接受少于16位但不接受更多

const card=  /^[0-9]*$/

提前感谢!

/^[0-9]{16}$/->精确的16位

/^[0-9]{16,20}$/-->在16到20位之间

/^[0-9]{16,}$/-->最小16位,无上限

/^[0-9]{0,16}$/-->最多16位

您可以使用的16位数字:

const card = /^'d{16}$/;

const card = /^[0-9]{16}$/;

少于16位或您可以使用的16位:

const card = /^'d{0,16}$/;

const card = /^[0-9]{0,16}$/;

您可以使用卷曲制动器来定义有效出现次数。使用'd表示数字-它是[0-9]:的同义词

window.alert('123456789012345'.match(/^'d{0,15}$/)); // is OK
window.alert('1234567890123456'.match(/^'d{0,15}$/)); // is not OK

试试看:http://www.regextester.com/?fam=93816