【发布时间】:2018-08-15 22:32:55
【问题描述】:
我正在寻找一种方法来验证信用卡模式的开始。以万事达卡为例。
上面写着(参考:https://www.regular-expressions.info/creditcard.html):
万事达卡号码以 51 到 55 开头...
我正在寻找当用户输入时返回 true 的正则表达式:
const regex = /^5|5[1-5]/; // this is not working :(
regex.test("5"); // true
regex.test("51"); // true
regex.test("55"); // true
regex.test("50"); // should be false, but return true because it matches the `5` at the beginning :(
【问题讨论】:
-
好吧,如果是
51-55,你为什么允许5?您的第二个正则表达式模式的选项将永远被满足(实际上会,但在信用卡号的任何位置,而不是在开头)。 -
只要使用
^5[1-5] -
万事达卡也以其他 BIN 号码开头。
-
见@revilheart 答案。我正在寻找一种在用户输入时验证值的方法。感谢您的帮助。
标签: javascript regex credit-card