Javascript 正则表达式检测可能的信用卡号码

发布于 2024-07-27 16:54:01 字数 491 浏览 4 评论 0原文

我在想出一个合适的正则表达式或一组正则表达式时遇到了无穷无尽的麻烦。

我想做的是检测:

  • 检测长度为 13 到 19 的数字的连续运行
  • 检测长度为 13 到 19 的空格散布的数字的连续运行
  • 检测长度为 13 到 19 的破折号散布的数字的连续运行

基本业务要求是警告用户他们可能在文本字段中输入了信用卡号,但他们不应该这样做(尽管只是警告,而不是硬错误)。 文本字段可以跨越多行,最长可达 8k,CC # 可以嵌入到任何地方(不太可能跨多行分割),可以超过 1 CC#(尽管检测到比例至少 1 就是我所需要的,我不需要实际值)。 不需要验证校验位。

长度检查可以在外部完成...即我很乐意循环一组匹配,删除任何空格/破折号,然后进行长度比较。

但是... JavaScript 正则表达式挫败了我的每一次尝试(只是不在正确的“顶部空间”)所以我想我应该在这里问:)

谢谢!

I'm having no end of trouble coming up with an appropriate regex or set of regex's.

What I want to do is detect:

  • Detect contineous run of digits of length 13 through 19
  • Detect contineous run of digits interspersed with whitespace of length 13 through 19
  • Detect contineous run of digits interspersed with dashes of length 13 through 19

The basic business requirement is to warn a user that they may have entered a credit card number in a text field and they ought not to do that (though only a warning, not a hard error). The text field could span multiple lines, could be up to 8k long, a CC # could be embedded anywhere (unlikely to split across multiple lines), could be more than 1 CC# (though detecting the prescence of at least 1 is all I need. I don't need the actual value). Don't need to validate check digit.

The length check can be done external... ie I'm happy to loop through a set of matches, drop any whitespace/dashes and then do a length comparison.

But... JavaScript regex is defeating my every attempt (just not in the right "head space") so I thought I'd ask here :)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

淡笑忘祈一世凡恋 2024-08-03 16:54:01

以下是信用卡验证所需的所有规则。 您应该能够在 Javascript 中轻松完成此操作。

Here are all the rules required for credit card validation. You should be able to easily do this in Javascript.

无需解释 2024-08-03 16:54:01

看起来像一个相当简单的正则表达式。 我已经让您的要求更加严格了 - 事实上,您需要匹配日期列表,例如“1999-04-02 2009-12-09 2003-11-21”。 我假设这些部分将分为三到六组,并且它们本身将是三到八个数字/破折号/空格的组。 您可以相当容易地调整这些数字。

/[0-9]{13,19}|([0-9- ]{3,8}){3,6}/

我不确定这是否是您想要的,但我认为值得一试。 如果这不是您想要的,也许您可​​以向我们展示一些接近或给人留下您想要的印象的正则表达式?

Seems like a fairly simple regex. I've made your requirements a little more stringent - as it was, you'd match lists of dates, such as "1999-04-02 2009-12-09 2003-11-21". I've assumed the sections will be in three to six groups and will themselves be groups of three to eight numbers/dashes/whitespace. You can tune these numbers fairly easily.

/[0-9]{13,19}|([0-9- ]{3,8}){3,6}/

I'm not sure this is what you want, but I figured it was worth a go. If this isn't what you're looking for, perhaps you could show us some regexes that come close or give an impression of what you want?

提笔落墨 2024-08-03 16:54:01

您想使用 Javascript 正则表达式 8k 文本吗? 不要浪费你的时间(或者用户可怜的CPU)用Javascript来做这件事。 无论如何,您都需要服务器端验证,所以就这样吧。

You want to regex 8k of text with Javascript? Don't waste your time (or the users poor CPU) doing this with Javascript. You're gonna need server-side validation anyways, so just leave it to that.

浴红衣 2024-08-03 16:54:01

这是一个适用于许多信用卡验证步骤的出色脚本。

http://javascript.internet.com/forms/val-credit-card。 html

Here is a great script for many credit card validation steps.

http://javascript.internet.com/forms/val-credit-card.html

乖乖兔^ω^ 2024-08-03 16:54:01
/(?:\d[ -]?){12,18}\d/

检查 13-19 位数字,每个数字(最后一位除外)后面可能有空格或破折号。 不过,这是一个非常自由的正则表达式,您可能希望将其范围缩小到已知的实际信用卡格式。

/(?:\d[ -]?){12,18}\d/

Checks for 13-19 digits, each of which digit (except the last) may have a space or dash after it. This is a very liberal regex, though, and you may want to narrow it down to known actual credit card formats.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文