如何使用包含空格的信用卡号码?

发布于 2024-07-20 01:09:23 字数 108 浏览 4 评论 0原文

当检测到未经培训的购物者输入了带有空格的信用卡/借记卡号时,一些精美的网站会显示错误对话框。 是否有可能以某种方式编写一个 Java Web 应用程序来处理这些带有空格的数字,就好像它们是正确的一样?

Some fancy websites show an error dialog when it is detected that an untrained shopper has entered a credit/debit card number as it is printed on their card with spaces. Is it possible in some way to write a Java web app that handles these numbers with spaces as if they were correct?

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

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

发布评论

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

评论(18

番薯 2024-07-27 01:09:23

我的观点是,任何拒绝带有空格的信用卡号的 Web 应用程序都没有发挥作用。 当您收到信用卡号时,很容易做到:

String ccNumber = ccNumber.replaceAll("[\\s-]+", "");

删除空格和破折号(有些也使用这些)。 然后验证结果。 如果您强迫用户删除您可以轻松做到的空格,您只会惹恼他们。

至于如何进行验证,这取决于很多因素,例如您正在使用哪个 Web 框架以及您选择了哪些验证选项。 例如,Struts 1 可能会或可能不会使用 Apache Commons Validator,而 Spring MVC 将(可能)使用 Spring 验证等等。 因此,我无法确切地告诉您如何验证,但我可以告诉您要验证的内容

首先,带有空格的 CC 号码不应被拒绝。 大多数人会发现:

4123 0987 8876 2939

比:更容易阅读

4123098788762939

如果用户漏掉或输错了数字,并且需要找出他或她的信用卡号验证失败的原因,这一点非常重要。 本文顶部的replaceAll() 涵盖了这种情况。

第二件事是您以正确的方式显示信用卡号(即使出于安全原因某些数字被替换为 X)。 我建议您仔细阅读信用卡号码剖析

该页面为您提供了位数和有效前缀的规则。 强大的 Web 应用程序将实现这些以便您可以在尝试使用信用卡号之前判断它是否无效。 将信用卡详细信息提交到支付网关可能需要长达 30 秒(或可能更长时间)的时间,因此在您确信付款将被接受之前,请勿执行此操作。 否则就会提供非常糟糕的用户体验。 如果失败 1-2 次,用户很可能会放弃而不是等待。

至于显示它们,取决于位数:

  • 16:4组,每组4个,用空格分隔;
  • 15:就像一张 美国运通 卡,即 4-6 -5,每组之间有一个空格;
  • 14:就像一张 Diners Club 卡,即 4-6-4每组之间的空间;
  • 13:从未见过 13,但我会想到 4-5-4 或 4-4-5 或 5-4-4(或可能是 3-3-3-4)。

作为标准验证例程的一部分,在提交处理之前,应根据页面中提到的校验和算法对信用卡号进行验证。 该页面有该例程的 Java 实现。

每个接受信用卡付款的网站都应该绝对最低限度执行上述所有操作,否则您就会因为一定比例的用户感到沮丧而放弃业务。

因此,简短的版本是两个简单的规则:

  1. 对用户输入尽可能宽容; 在提交之前尽
  2. 一切可能验证信用卡详细信息。

My view is that any Web app that rejects a credit card number with spaces isn't doing its job. When you receive a credit card number, it's easy enough to do:

String ccNumber = ccNumber.replaceAll("[\\s-]+", "");

to remove spaces and dashes (some use those too). Then validate the result. You'll simply annoy your users if you force them to remove spaces you could just as easily do.

As for how to validate, well that depends on a lot of things, such as which Web framework you're using and what validation options you've chosen. Struts 1 for example might or might not use Apache Commons Validator whereas Spring MVC will (probably) use Spring validation and so on. So I can't tell you exactly how to validate but I can tell you what to validate.

The first thing is that a CC number with spaces should not be rejected. Most people will find:

4123 0987 8876 2939

much easier to read than:

4123098788762939

which is really important if the user misses or mistypes a digit and needs to find why his or her credit card number failed validation. The replaceAll() at the top of this post covers this situation.

The second thing is that you display the credit card number (even when some of the digits are replaced with X for security reasons) in the correct way. I suggest you read through Anatomy of Credit Card Numbers.

That page gives you the rules for the number of digits and the valid prefixes. A robust Web application will implement these so you can tell if a credit card number is invalid before you try and use it. It can take up to 30 seconds (or possibly more) to submit credit card details to a payment gateway so you shouldn't do it until you are sure as you can be that the payment will be accepted. To do otherwise is to provide a really bad user experience. There is every chance the user will give up if it fails 1-2 times rather than wait.

As for displaying them, that depends on the # of digits:

  • 16: 4 groups of 4 separated by a space;
  • 15: like an American Express card ie 4-6-5 with a space between each group;
  • 14: like a Diners Club card ie 4-6-4 with a space between each group;
  • 13: Never seen 13 but 4-5-4 or 4-4-5 or 5-4-4 (or possibly 3-3-3-4) springs to mind.

The credit card number should be verified according to the checksum algorithm mentioned in the page before submitting for processing as part of a standard validation routine. That page has a Java implementation of that routine.

Every website that accepts credit card payment should be doing all of the above as an absolute minimum or you're simply throwing away business as a percentage of your users get frustrated.

So the short version is two simple rules:

  1. Be as forgiving as possible with user input; and
  2. Do absolutely everything possible to validate credit card details prior to submission.
终难愈 2024-07-27 01:09:23

我会尽可能删除所有非数字字符,然后在通过实际验证运行用户输入之前检查长度是否有效,例如 Luhn 算法

String ccNumber = input.replaceAll("\\D", "");

字符串输入中去除所有非数字。

I would go as far as stripping out all non-numeric characters then checking that the length is valid before running the user input through real validation like Luhn's algorithm.

String ccNumber = input.replaceAll("\\D", "");

strips out all the non-digits from String input.

鱼窥荷 2024-07-27 01:09:23

强迫您以特定格式输入信用卡号码(和类似内容)的网站 - 严重惹恼了我。

这些人给客户带来不便仅仅是因为他们(开发人员)很懒。 没有理由不接受信用卡号、电话号码等任何格式的信息。 唯一的限制是需要了解如何解释该值。

你不应该关心我输入的是 5555-4444-3333-2222 还是 5555444433332222,如果你不喜欢它们,只需将破折号去掉即可 - 与空格相同。 对于电话号码,除非您要自动拨打该号码,否则您可能甚至不关心它的格式,因此除非必要,否则不要惹恼您的用户。

Websites that force you to enter credit card numbers (and similar things) in a specific format - seriously annoy me.

Those people are inconveniencing their customers simply because they (the developers) are lazy. There is no reason not to accept things like credit card numbers, phone numbers, etc in whatever format they are provided. The only limitation is what is REQUIRED to understand how to interpret the value.

You shouldn't care whether I enter 5555-4444-3333-2222 or 5555444433332222, just strip the dashes out if you don't like them - same with spaces. And with phone numbers, unless you are going to be auto-dialing the number, you probably don't even care what format its in so don't annoy your users unless you have to.

┾廆蒐ゝ 2024-07-27 01:09:23

很不幸的是,不行。 Java 无法处理这些要求,因为在 x86 芯片上模拟 Java 虚拟机涉及太多开销,没有为像 Perl 的正则表达式这样的有用结构留下空间,而 Perl 的正则表达式可以做到这一点:

$input =~ s/\D//g;

Java 尝试将正则表达式添加到 a 中。几年前,但它们仅在 PowerPC 芯片上运行,但现在已不再使用。 问题是所有正则表达式都必须包含为字符串,而不是成为一流的语言构造,因此需要使用双反斜杠,但众所周知,反斜杠在 x86 架构的主操作系统上意味着不同的东西。

我的建议是升级到 Perl。 众所周知,Scheme 能够处理这种情况,并在竞争中提供巨大的优势,但Scheme 只能在 LISP 机器上运行。

Unfortunately, no. Java just cannot handle these requirements, since there is so much overhead involved in emulating the Java Virtual Machine on x86 chips, leaving no room for useful constructs like Perl's regular expressions, which can do it thusly:

$input =~ s/\D//g;

Java made an attempt at adding regular expressions a few years back, but they only ran on PowerPC chips, which are no longer used. The problem was that all regular expressions had to be contained as Strings instead of being a first class language construct, and thus doubling backslashes was required, but as everyone knows backslashes mean something different on the primary operating system for the x86 architecture.

My advice is to upgrade to Perl. Scheme is also known to be able to handle this situation as well as give a tremendous advantage over your competition, but Scheme runs only on LISP machines.

無處可尋 2024-07-27 01:09:23

不幸的是不是,这就是为什么那些精美的网站需要向未经训练的购物者显示错误对话框:强制购物者以机器喜欢的格式重新输入他们的号码。

为什么,如果只有机器可以进行“数据处理”,以便机器本身可以改变数据格式! 或者,如果不存在“未经训练的”购物者就好了! 唉!

Unfortunately not, which why instead those fancy web sites need to show an error dialog to the untrained shopper: to force the shopper to re-enter their number, in the format that the machine prefers.

Why, if only a machine could do "data processing", so that the machine itself could change the data format! Or, if only if there were no such thing as an "untrained" shopper! Alas!

樱娆 2024-07-27 01:09:23

你的问题看起来很奇怪,但我认为它应该像通过验证功能运行用户输入的信用卡号一样简单,该验证功能首先会删除所有空格。

对于任何使用或不使用正则表达式的现代语言来说,这都是相当微不足道的。

Your question seems strange but I would think that it should be as easy as running the credit card numbers entered by users through a validation function which would first of all remove all white spaces.

This is rather trivial in any modern language with or without using regex.

风情万种。 2024-07-27 01:09:23

简单的。

  • 您的输入空间是包含所有字符的某个字符集中的字符列表。
  • 您的输出空间是某个仅包含数字的字符集中的字符列表。

为了解决这个问题,我们创建一个仅包含数字 0 到 9 的中间空间。我们可以为这个有限集创建一个新的枚举。 我们将其称为手指空间,因为奇怪的是它包含的成员数量与我们手指的数量相同。

然后我们编写两个函数。

  1. 将输入空间转换为手指空间 将
  2. 手指空间转换为输出空间

当我们将输入空间减少为手指空间时,我们只是删除在手指空间中找不到的任何字符。 从手指空间转换到输出空间更加容易。 我们只是在输出空间中找到相同的数字。

诀窍是它适用于所有字符集。 我还没有弄清楚如何确定某个字符是否与我的手指集中的成员匹配。 也许我应该将其作为一个问题发布。

Easy.

  • Your input space is a list of characters from some character set containing all characters.
  • Your output space is a list of characters from some character set containing only numbers.

To solve this problem, we create an intermediate space containing only the numbers 0 to 9. We can create a new enum for this finite set. We'll call this our finger space, since it oddly contains the same number of members as we do fingers.

We then write two functions.

  1. Convert input space to finger space
  2. Convert finger space to output space

As we reduce the input space to the finger space, we just drop any character not found in the finger space. Converting from finger space to output space is even easier. We just find the same number in the output space.

The trick is for this to work with all character sets. I haven't figured out how to determine if a certain character matches a member in my finger set. Maybe I should post it as a question.

生来就爱笑 2024-07-27 01:09:23

Tom,

技术上问题解决了,我们来谈谈理论上吧。

这里有两种思想流派。 我不认为“如果用户无法弄清楚这是他们的问题”是可以接受的答案。

  1. 对用户输入保持坚定,只接受格式正确的信用卡号码。 这需要让用户一直停留在页面上,直到他们一切都正确为止。
  2. 通过假设他们的意图并为他们调整他们的输入来更加宽容(只需确保给他们一个确认屏幕来验证新输入)。

在我看来,#2 是可行的方法,您可以使用正则表达式(如上所述)将所有空格、特殊字符等从 cc# 字段中提取出来,并使用户不必再次输入其信息。

无论哪种方式,您都应该告知用户正确的输入形式(即 xxxx-xx-xxxx)

作为经验法则,我倾向于欣赏那些在处理用户输入的方式上更加优雅的网站。

有关正则表达式的更多提示,请查看正则表达式。 信息

祝你好运,

-罗伯特

Tom,

The problem is solved technically, let's talk about it theoretically.

There are two schools of thought here. I do not think it is an acceptable answer to say "if the user can't figure it out it's their problem."

  1. Be firm about your user input and only accept credit card numbers that are well-formed. This requires keeping the user on the page until they get everything right.
  2. Be more lenient by assuming their intentions and adjusting their input for them (just be sure to give them a confirmation screen to verify the new input).

In my opinion, #2 is the way to go, you can use regular expressions (as stated above) to pull all spaces, special characters, etc. out of the cc# field and keep the user from having to enter their information again.

Either way you should inform the user of the proper input form (i.e. xxxx-xx-xxxx)

as a rule of thumb, I tend to appreciate sites that are more elegant about the way they handle user input.

For more tips on regular expressions check out regular-expressions . info

Good luck,

-Robert

谁许谁一生繁华 2024-07-27 01:09:23

有很多选项,但最合乎逻辑的似乎是只进行简单的字符串替换,以用闭合字符(即“”)替换所有空格。 这会将信用卡字符串减少为一长串数字..然后处理掉

There are a number of options, but the most logical seems to be to just do a simple string replace to replace all the spaces with a closed character i.e. ''. This will reduce the credit card string to just one long string of numbers..then just process away

蓝眼泪 2024-07-27 01:09:23

该网站上的广告... 只需 49.95 美元,您就可以获得与该在线商店兼容的新型特殊键盘。 单击此处将新键盘添加到您的购物车并结账。 结帐时,请在指定字段中输入您的信用卡号码。 请不要输入数字之间的空格,因为我们的商店不知道如何处理数字之间的空格。

Advertisement on this website... For just $49.95 you can have a new special keyboard compatible with this online store. Click here to add the new keyboard in your cart and checkout. When checking out please enter your credit card number in the designated field. Please do not enter the spaces between the numbers as our store doesn't know how to deal with spaces between the numbers.

岛徒 2024-07-27 01:09:23

我假设这是一个真正的问题,尽管它看起来像是某种巨魔或笑话。

您应该对界面进行建模,以便用户本能地以受控方式执行输入。 简而言之,首先询问他们卡片的类型,然后在输入表单上格式化输入以匹配卡片。 例如,假设 Visa 或 Mastercard 等 16 位卡,显示 4 个由空格或破折号分隔的输入框,每个输入框限制输入 4 个字符,并在用户键入第四个数字后自动移至系列中的下一个框。

页面上的内容应类似于以下内容:

卡号:
[1234] - [1234] - [1234] - [1234]

卡号:
[1234] - [123456] - [12345]

I'm going to assume this is a real question even though it looks like some sort of troll or joke.

You should model your interface so that the user instinctively performs the input in a controlled manner. Simply put, ask them for the kind of card first, and then on the input form format the input to match the card. For example, assuming a 16 digit card like Visa or Mastercard, display 4 input boxes separated by spaces or dashes that limit input to 4 characters each and automatically move to the next box in the series after the user types the fourth digit.

It should look something like the following on the page:

Card Number:
[1234] - [1234] - [1234] - [1234]

or

Card Number:
[1234] - [123456] - [12345]

—━☆沉默づ 2024-07-27 01:09:23

似乎只有一个人提到了 Luhn 或 mod 10 算法

http://en.wikipedia.org/wiki /Luhn_算法

only one person seems to have mentioned the Luhn or mod 10 algorithm

http://en.wikipedia.org/wiki/Luhn_algorithm

千紇 2024-07-27 01:09:23

当然。 压缩空间。 可能有无数种方法可以做到这一点; 我很想使用 String.split() 将其分解为空格,然后连接得到的四个字符串。 或者使用 StringBuffer.indexOf() 和 .delteCharAt()。

...或者正如 Cletus 所说,使用replaceAll()。

Sure. Compress out the spaces. There are probably a zillion ways to do it; I'd be tempted to use String.split() to break it up on the spaces, then concatente the four strings resulting. Or use StringBuffer.indexOf() and .delteCharAt().

...or as Cletus said, use replaceAll().

迷爱 2024-07-27 01:09:23

您可以通过使用 onkeypress 事件来使用 javascript 验证来检查最后一个字符是否有效,如果不是则将其删除,甚至可能会弹出一条消息,指出输入了无效字符。 这样就永远不会输入无效的数字。 它还可以按照您想要的格式自动输入间隔字符(空格或 -)。

you could use javascript validation by using the onkeypress event to check if the last character is valid and if not just remove it and maybe even flash up a message saying that an invalid character was entered. This way invalid numbers are never entered. It could also automatically enter a spacer character (space or -) in the format you want.

剧终人散尽 2024-07-27 01:09:23

早在 1998 年,我就在一家只允许使用 Visa 的商店(……实际上是 Visa)编写了这对 Perl 函数。

sub mod10_checkdigit
{
    my($acct) = @_;
    die "invalid account number in BRPS::mod10_checkdigit"
        unless $acct =~ m%^\d+$%;
    my(@digits) = split //, $acct;
    my($len) = scalar(@digits);
    print "# ($len) @digits\n" if ($ENV{PERL_BRPS_DEBUG});
    my($i, $sum, $chk);
    my($mul) = (($len % 2) == 1) ? 1 : 2;
    $len--;
    for ($i = 0; $i < $len; $i++)
    {
        my($val) = $mul * $digits[$i];
        # Note that we need the digital root of the value, but $val is not
        # greater than 18 (because $digits[$i] <= 9 and $mul <= 2).
        $val -= 9 if ($val >= 10);
        $sum += $val;
        print "# $i: $digits[$i] * $mul => $val => $sum\n" if ($ENV{PERL_BRPS_DEBUG});
        $mul = 3 - $mul;
    }
    $chk = 10 - ($sum % 10);
    $chk = 0 if ($chk == 10);
    return $chk;
}

sub validate_account
{
    my($acct) = @_;
    # Strip leading and trailing blanks
    $acct =~ s/^\s*(\S.*\S)\s*$/$1/;
    my($clean) = $acct;
    # Check that account number is string of digits, blanks and dashes
    return undef, "account number is not a sequence of digits, blanks and dashes"
        unless $acct =~ m/^[- \d]+$/;
    return undef, "account number is not a Visa account number"
        unless $acct =~ m/^4/;
    # Remove non-digits
    $clean =~ s/\D//g;
    return undef, "account number is neither 13 nor 16 digits"
        unless length($clean) == 16 || length($clean) == 13;
    # Punctuators must be reasonably consistent!
    return undef, "invalid punctuation pattern"
        unless ($acct =~ m/^\d{16}$/o or $acct =~ m/^\d{13}$/o or
                $acct =~ m/^\d{4}[- ]\d{4}[- ]\d{4}[- ]\d{4}$/o or
                $acct =~ m/^\d{4}[- ]\d{3}[- ]\d{3}[- ]\d{3}$/o);
    # Determine check digit
    my($chk) = mod10_checkdigit($clean);
    return undef, "check digit on account number is incorrect"
        unless $clean =~ m/$chk$/;
    return $clean, "ok";
}

它们允许可信的信用卡号码通过。 概括起来,也可以处理万事达卡、发现卡、美国运通卡。

咆哮

我不喜欢那些坚持要求我以内部格式输入数据的网站。 该死的——将数字存储为一个大整数并将其作为纯数字字符串发送; 这对电脑来说没问题。 但请让我输入可识别的人类可读格式 - 甚至以人类可读格式重新呈现数据。 处理信用卡号码的网站实在是太懒了。

I wrote this pair of Perl functions in a shop where only Visa was allowed (...at Visa, actually...) back in 1998.

sub mod10_checkdigit
{
    my($acct) = @_;
    die "invalid account number in BRPS::mod10_checkdigit"
        unless $acct =~ m%^\d+$%;
    my(@digits) = split //, $acct;
    my($len) = scalar(@digits);
    print "# ($len) @digits\n" if ($ENV{PERL_BRPS_DEBUG});
    my($i, $sum, $chk);
    my($mul) = (($len % 2) == 1) ? 1 : 2;
    $len--;
    for ($i = 0; $i < $len; $i++)
    {
        my($val) = $mul * $digits[$i];
        # Note that we need the digital root of the value, but $val is not
        # greater than 18 (because $digits[$i] <= 9 and $mul <= 2).
        $val -= 9 if ($val >= 10);
        $sum += $val;
        print "# $i: $digits[$i] * $mul => $val => $sum\n" if ($ENV{PERL_BRPS_DEBUG});
        $mul = 3 - $mul;
    }
    $chk = 10 - ($sum % 10);
    $chk = 0 if ($chk == 10);
    return $chk;
}

sub validate_account
{
    my($acct) = @_;
    # Strip leading and trailing blanks
    $acct =~ s/^\s*(\S.*\S)\s*$/$1/;
    my($clean) = $acct;
    # Check that account number is string of digits, blanks and dashes
    return undef, "account number is not a sequence of digits, blanks and dashes"
        unless $acct =~ m/^[- \d]+$/;
    return undef, "account number is not a Visa account number"
        unless $acct =~ m/^4/;
    # Remove non-digits
    $clean =~ s/\D//g;
    return undef, "account number is neither 13 nor 16 digits"
        unless length($clean) == 16 || length($clean) == 13;
    # Punctuators must be reasonably consistent!
    return undef, "invalid punctuation pattern"
        unless ($acct =~ m/^\d{16}$/o or $acct =~ m/^\d{13}$/o or
                $acct =~ m/^\d{4}[- ]\d{4}[- ]\d{4}[- ]\d{4}$/o or
                $acct =~ m/^\d{4}[- ]\d{3}[- ]\d{3}[- ]\d{3}$/o);
    # Determine check digit
    my($chk) = mod10_checkdigit($clean);
    return undef, "check digit on account number is incorrect"
        unless $clean =~ m/$chk$/;
    return $clean, "ok";
}

The allow plausible credit card numbers through. It wouldn't be hard to generalize to handle Mastercard, Discover, American Express too.

Rant

I do not like web sites that insist on me entering data in their internal format. Dammit - store the number as a large integer and send it as a pure digit string; that's fine for computers. But do let me enter recognizable human legible formats - even re-present the data in the human-legible format. There is far, far, far too much laziness in web sites that handle credit card numbers.

深居我梦 2024-07-27 01:09:23

如果您指的是 javascript,您可以使用“前进到下一个输入”方法:

这是 HTML:

<form id="ccform" action="cc_submit.php" method="post">
    <fieldset id="ccnumber">
        <input id="firstset" type="text" maxlength="4" />
        <input id="secondset" type="text" maxlength="4" />
        <input id="thirdset" type="text" maxlength="4" />
        <input id="fourthset" type="text" maxlength="4" />
    </fieldset>
</form>

这是 JS:

var ccfields;

function moveToNext(e) {
    var field = e.currentTarget;
    var chars = field.value.length;
    var setnumb = Number(field.id.substr(3,1)) - 1;
    if(chars >= 4 && setnumb < 3) {
        ccfields[setnumb + 1].focus();
    }
}

window.onload = function() {
    ccfields = document.getElementById("ccnumber").getElementsByTagName("input");
    for (var i = 0; i < ccfields.length; i++) {
        ccfields[i].onkeyup = moveToNext;
    }
};

当然,您需要添加一个检查非数字的函数和一个用于获取四个值的函数字段并将它们合并成一个字符串以传回表单。 使用像 Jquery 这样的 js 库来确保以相同的方式处理事件并简化输入的遍历也不是一个坏主意,这样您就可以使用像“name”这样的属性而不会产生任何混淆。

但一般来说,如果人们看到 4 个字段,则可以更轻松地输入他们的号码,对于那些认为“啊疯了,我必须使用鼠标输入每个号码”的访客来说,他们很高兴(或者至少我很高兴)该页面足够智能,知道要移至下一个字段。

If you mean javascript, you could go with the "advance to next input" method:

Here's the HTML:

<form id="ccform" action="cc_submit.php" method="post">
    <fieldset id="ccnumber">
        <input id="firstset" type="text" maxlength="4" />
        <input id="secondset" type="text" maxlength="4" />
        <input id="thirdset" type="text" maxlength="4" />
        <input id="fourthset" type="text" maxlength="4" />
    </fieldset>
</form>

And here's the JS:

var ccfields;

function moveToNext(e) {
    var field = e.currentTarget;
    var chars = field.value.length;
    var setnumb = Number(field.id.substr(3,1)) - 1;
    if(chars >= 4 && setnumb < 3) {
        ccfields[setnumb + 1].focus();
    }
}

window.onload = function() {
    ccfields = document.getElementById("ccnumber").getElementsByTagName("input");
    for (var i = 0; i < ccfields.length; i++) {
        ccfields[i].onkeyup = moveToNext;
    }
};

Of course, you will want to add a function that checks for non-numbers and a function for taking the four fields and merging them into one string to pass back to the form. It also isn't a bad idea to use a js library like Jquery to ensure that events are handled the same way and simplify traversing through the inputs so you can use attributes like "name" without any confusion.

But generally, if people see 4 fields, it makes it easier to type in their number, and for those visitors who think "ah nuts, I have to use my mouse for each number" they are (or at least I am) pleased that the page is smart enough to know to move to the next field.

少跟Wǒ拽 2024-07-27 01:09:23

解决方案 1:只需输入 4 个可容纳 4 位数字的文本框即可。 就像如何输入软件的许可证密钥一样。 输入空格字符或制表符后,您可以触发框更改为下一行。

解决方案 2:使用上述评论之一中提到的正则表达式。 问题是,如果它是一个 Web 应用程序,您将很容易受到注入攻击。

Solution 1: How about just put in 4 text boxes that can take 4 digit numbers. Like how the license keys for software's are entered. You can trigger the boxes to change to the next in line upon entering a space character or the tab character.

Solution 2: Use regular expressions as mentioned in one of the comments described above. Problem being, you will be susceptible to injection attacks if its a web application.

探春 2024-07-27 01:09:23

JavaTM 非常适合服务器端编程,而 javascript 在客户端可能很有用; 例如在验证期间。

有效信用卡号的长度在 12(例如 Maestro)和 19(例如 Solo、Switch)之间。 客户端 JavaScript 可以查明卡号是否有效(仅限数字(带破折号或空格)、适合发卡机构、校验和正常,...)并从“人类可读”执行 1:1 映射(例如

American Express    3400 0100 2000 009

)到内部表示,例如,

<input ... id="ccid" value="340001002000009">
<input ... id="ccissuer" value="AMEX">

一旦信用卡信息验证通过了输入期间的验证,这些值就可以透明地转换为内部表单提交时

可以在网上找到一个对此事进行一些检查的普通 JavaScript 函数的示例,例如这里 http://www.braemoor.co.uk/software/creditcard.shtml

关于答案的描述就这么多。

JavaTM is great for server-side programming, while javascript is may be useful on the client side; for example during validation.

A valid credit card number varies in length between 12 (e.g. Maestro) and 19 (e.g. Solo, Switch). The client-side javascript could find out whether the card number is valid (only digits (with dashes or whitespace), suits the issuer authority, checksum's ok, ...) and perform a 1:1 mapping from a 'human-readable' (e.g.

American Express    3400 0100 2000 009

) to a internal representation, e.g.

<input ... id="ccid" value="340001002000009">
<input ... id="ccissuer" value="AMEX">

Once the credit card information validation has passed the validation during input, the values can be transparently converted into the internal form on-submit.

An example of a plain javascript function that does some checks on that matter may be found on the net, e.g. here http://www.braemoor.co.uk/software/creditcard.shtml.

So much for a description of an answer.

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