简体   繁体   English

信用卡号码验证

[英]Credit Card Number Validation

Hello I am having trouble with my javascript validation.您好,我的 javascript 验证遇到问题。 I am trying to validate a credit card number which I want it to display between 13 and 18 numbers and single spaces.我正在尝试验证一个信用卡号,我希望它显示在 13 到 18 个数字和单个空格之间。

My Javascript code:我的 Javascript 代码:

<script type="text/javascript">
function validateForm()
{
var x=document.forms["checkout_details"]["creditcard"].value;
    if(x.length != 13 || x.length != 18)//if the length is not 13 or 18
    {
        alert("Please enter a valid credit card number");//display message to user;
        return false;
    }    
}
</script>

HTML/PHP Code: HTML/PHP 代码:

echo '<form name="checkout_details" action="confirm.php" onsubmit="return validateForm()" method="post">';
echo '<font color="red">*</font>&nbsp;<b>Credit Card Number:</b> <input type="text" name="creditcard" id="creditcard"><br /><br />';
echo '<input type="submit" value="Purchase">';
echo '</form>';

Anyone help me out tell me what i'm doing wrong.任何人都帮我告诉我我做错了什么。 Thanks.谢谢。

Instead of 代替

if(x.length != 13 || x.length != 18)

you should use 你应该使用

if(x.length != 13 && x.length != 18)

in fact, with your original condition, 18 is different from 13, and that would have returned true ! 实际上,在您的原始状态下,18与13会不同,那将返回true

Also, that accepts only exact length of 13 or 18, if you want to reject anything shorter than 13 or longer than 18, then you need 另外,它仅接受13或18的确切长度,如果您想拒绝小于13或大于18的任何内容,则需要

if(x.length < 13 || x.length > 18)

You meant 你的意思是

if (x.length < 13 || x.length > 18) {
    ...
}

The original complains if the length is not exactly 13 and 18 characters; 原件抱怨长度不完全是13 18个字符; that is: always , as a string having length of 13 cannot have length of 18 and vice versa; 也就是说: 总是 ,因为长度为13的字符串不能为18,反之亦然; also you want to match all the lengths in between. 您也想匹配两者之间的所有长度。

Credit card validation is not that simple. 信用卡验证不是那么简单。 You should use a regex to check it. 您应该使用正则表达式进行检查。 There are also a large variety of credit card formats. 也有各种各样的信用卡格式。

You can see the different regex' here Credit card regex' 您可以在此处查看其他正则表达式信用卡正则表达式

不应该是:

if(x.length < 13 || x.length > 18)

Try this 尝试这个

if(x.length > 13 && x.length < 18) //if the length is between 13 and 18

There's also a very good plugin for CC validation here 这里还有一个非常好的CC验证插件

jquery credit card validator jQuery信用卡验证器

Validating credit cards is more than just the length of card number.验证信用卡不仅仅是卡号的长度。 To properly validate the card number (including the length), you should perform a check against the Luhn Algorithm.要正确验证卡号(包括长度),您应该对 Luhn 算法进行检查。 Use this code:使用此代码:

 const options = { method: 'GET', headers: {Accept: 'application/json', 'X-Api-Key': '[APIkey]'} }; fetch('https://api.epaytools.com/Tools/luhn?number=[CardNumber]&metaData=true', options).then(response => response.json()).then(response => console.log(response)).catch(err => console.error(err));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM