如何使用包含空格的信用卡号? [英] How can I use credit card numbers containing spaces?

查看:160
本文介绍了如何使用包含空格的信用卡号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

我的视图是任何拒绝带空格的信用卡号的Web应用程序都无法正常工作。当您收到信用卡号码时,这很容易做到:

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

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



至于如何验证,以及这取决于很多事情,例如您正在使用的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. 在提交之前尽一切可能验证信用卡详细信息。


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?

解决方案

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.

这篇关于如何使用包含空格的信用卡号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆