我只想在 JavaScript 代码中验证信用卡号。我对数字使用了正则表达式,但我不知道为什么它不起作用!
下面是我的函数:
function validate_creditcardnumber()
{
var re16digit = /^\d{16}$/
if (document.myform.CreditCardNumber.value.search(re16digit) == -1)
alert("Please enter your 16 digit credit card numbers");
return false;
}
最佳答案
希望以下两个链接能帮助解决您的问题。
仅供引用,世界上有多种信用卡可用。所以,你的想法是错误的。信用卡有一些格式。请参阅以下链接。第一个是纯 JavaScript,第二个是使用 jQuery。
2022 年 5 月 17 日更新
演示:
var ccErrorNo = 0;
var ccErrors = new Array ()
ccErrors [0] = "Unknown card type";
ccErrors [1] = "No card number provided";
ccErrors [2] = "Credit card number is in invalid format";
ccErrors [3] = "Credit card number is invalid";
ccErrors [4] = "Credit card number has an inappropriate number of digits";
ccErrors [5] = "Warning! This credit card number is associated with a scam attempt";
function checkCreditCard (cardnumber, cardname) {
// Array to hold the permitted card characteristics
var cards = new Array();
// Define the cards we support. You may add addtional card types as follows.
// Name: As in the selection box of the form - must be same as user's
// Length: List of possible valid lengths of the card number for the card
// prefixes: List of possible prefixes for the card
// checkdigit: Boolean to say whether there is a check digit
cards [0] = {name: "Visa",
length: "13,16",
prefixes: "4",
checkdigit: true};
cards [1] = {name: "MasterCard",
length: "16",
prefixes: "51,52,53,54,55",
checkdigit: true};
cards [2] = {name: "DinersClub",
length: "14,16",
prefixes: "36,38,54,55",
checkdigit: true};
cards [3] = {name: "CarteBlanche",
length: "14",
prefixes: "300,301,302,303,304,305",
checkdigit: true};
cards [4] = {name: "AmEx",
length: "15",
prefixes: "34,37",
checkdigit: true};
cards [5] = {name: "Discover",
length: "16",
prefixes: "6011,622,64,65",
checkdigit: true};
cards [6] = {name: "JCB",
length: "16",
prefixes: "35",
checkdigit: true};
cards [7] = {name: "enRoute",
length: "15",
prefixes: "2014,2149",
checkdigit: true};
cards [8] = {name: "Solo",
length: "16,18,19",
prefixes: "6334,6767",
checkdigit: true};
cards [9] = {name: "Switch",
length: "16,18,19",
prefixes: "4903,4905,4911,4936,564182,633110,6333,6759",
checkdigit: true};
cards [10] = {name: "Maestro",
length: "12,13,14,15,16,18,19",
prefixes: "5018,5020,5038,6304,6759,6761,6762,6763",
checkdigit: true};
cards [11] = {name: "VisaElectron",
length: "16",
prefixes: "4026,417500,4508,4844,4913,4917",
checkdigit: true};
cards [12] = {name: "LaserCard",
length: "16,17,18,19",
prefixes: "6304,6706,6771,6709",
checkdigit: true};
// Establish card type
var cardType = -1;
for (var i=0; i<cards.length; i++) {
// See if it is this card (ignoring the case of the string)
if (cardname.toLowerCase () == cards[i].name.toLowerCase()) {
cardType = i;
break;
}
}
// If card type not found, report an error
if (cardType == -1) {
ccErrorNo = 0;
return false;
}
// Ensure that the user has provided a credit card number
if (cardnumber.length == 0) {
ccErrorNo = 1;
return false;
}
// Now remove any spaces from the credit card number
cardnumber = cardnumber.replace (/\s/g, "");
// Check that the number is numeric
var cardNo = cardnumber
var cardexp = /^[0-9]{13,19}$/;
if (!cardexp.exec(cardNo)) {
ccErrorNo = 2;
return false;
}
// Now check the modulus 10 check digit - if required
if (cards[cardType].checkdigit) {
var checksum = 0; // running checksum total
var mychar = ""; // next char to process
var j = 1; // takes value of 1 or 2
// Process each digit one by one starting at the right
var calc;
for (i = cardNo.length - 1; i >= 0; i--) {
// Extract the next digit and multiply by 1 or 2 on alternative digits.
calc = Number(cardNo.charAt(i)) * j;
// If the result is in two digits add 1 to the checksum total
if (calc > 9) {
checksum = checksum + 1;
calc = calc - 10;
}
// Add the units element to the checksum total
checksum = checksum + calc;
// Switch the value of j
if (j ==1) {j = 2} else {j = 1};
}
// All done - if checksum is divisible by 10, it is a valid modulus 10.
// If not, report an error.
if (checksum % 10 != 0) {
ccErrorNo = 3;
return false;
}
}
// Check it's not a spam number
if (cardNo == '5490997771092064') {
ccErrorNo = 5;
return false;
}
// The following are the card-specific checks we undertake.
var LengthValid = false;
var PrefixValid = false;
var undefined;
// We use these for holding the valid lengths and prefixes of a card type
var prefix = new Array ();
var lengths = new Array ();
// Load an array with the valid prefixes for this card
prefix = cards[cardType].prefixes.split(",");
// Now see if any of them match what we have in the card number
for (i=0; i<prefix.length; i++) {
var exp = new RegExp ("^" + prefix[i]);
if (exp.test (cardNo)) PrefixValid = true;
}
// If it isn't a valid prefix there's no point at looking at the length
if (!PrefixValid) {
ccErrorNo = 3;
return false;
}
// See if the length is valid for this card
lengths = cards[cardType].length.split(",");
for (j=0; j<lengths.length; j++) {
if (cardNo.length == lengths[j]) LengthValid = true;
}
// See if all is OK by seeing if the length was valid. We only check the length if all else was
// hunky dory.
if (!LengthValid) {
ccErrorNo = 4;
return false;
};
// The credit card is in the required format.
return true;
}
function testCreditCard() {
myCardNo = document.getElementById('CardNumber').value;
myCardType = document.getElementById('CardType').value;
if (checkCreditCard(myCardNo, myCardType)) {
alert("Credit card has a valid format")
} else {
alert(ccErrors[ccErrorNo])
};
}<!--
<script src="https://www.braemoor.co.uk/software/_private/creditcard.js"></script>
-->
<!-- COPIED THE DEMO CODE FROM THE SOURCE WEBSITE (https://www.braemoor.co.uk/software/creditcard.shtml) -->
<table>
<tbody>
<tr>
<td style="padding-right: 30px;">American Express</td>
<td>3400 0000 0000 009</td>
</tr>
<tr>
<td>Carte Blanche</td>
<td>3000 0000 0000 04</td>
</tr>
<tr>
<td>Discover</td>
<td>6011 0000 0000 0004</td>
</tr>
<tr>
<td>Diners Club</td>
<td>3852 0000 0232 37</td>
</tr>
<tr>
<td>enRoute</td>
<td>2014 0000 0000 009</td>
</tr>
<tr>
<td>JCB</td>
<td>3530 111333300000</td>
</tr>
<tr>
<td>MasterCard</td>
<td>5500 0000 0000 0004</td>
</tr>
<tr>
<td>Solo</td>
<td>6334 0000 0000 0004</td>
</tr>
<tr>
<td>Switch</td>
<td>4903 0100 0000 0009</td>
</tr>
<tr>
<td>Visa</td>
<td>4111 1111 1111 1111</td>
</tr>
<tr>
<td>Laser</td>
<td>6304 1000 0000 0008</td>
</tr>
</tbody>
</table>
<hr /> Card Number:
<select tabindex="11" id="CardType" style="margin-left: 10px;">
<option value="AmEx">American Express</option>
<option value="CarteBlanche">Carte Blanche</option>
<option value="DinersClub">Diners Club</option>
<option value="Discover">Discover</option>
<option value="EnRoute">enRoute</option>
<option value="JCB">JCB</option>
<option value="Maestro">Maestro</option>
<option value="MasterCard">MasterCard</option>
<option value="Solo">Solo</option>
<option value="Switch">Switch</option>
<option value="Visa">Visa</option>
<option value="VisaElectron">Visa Electron</option>
<option value="LaserCard">Laser</option>
</select> <input type="text" id="CardNumber" maxlength="24" size="24" style="margin-left: 10px;"> <button id="mybutton" type="button" onclick="testCreditCard();" style="margin-left: 10px; color: #f00;">Check</button>
<p style="color: red; font-size: 10px;"> COPIED THE DEMO CODE FROM TEH SOURCE WEBSITE (https://www.braemoor.co.uk/software/creditcard.shtml) </p>
关于javascript - 如何验证信用卡号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6176802/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为