本文实例讲述了Javascript验证Visa和MasterCard信用卡号的方法。分享给大家供大家参考。具体实现方法如下:
visa验证:
<HTML> <head> <title>Visa Example</title> <script type="text/javascript"> function luhnCheckSum(sCardNum) { var iOddSum = 0; var iEvenSum = 0; var bIsOdd = true; for (var i=sCardNum.length-1; i >= 0; i--) { var iNum = parseInt(sCardNum.charAt(i)); if (bIsOdd) { iOddSum += iNum; } else { iNum = iNum * 2; if (iNum > 9) { iNum = eval(iNum.toString().split("").join("+")); } iEvenSum += iNum; } bIsOdd = !bIsOdd; } return ((iEvenSum + iOddSum) % 10 == 0); } function isValidVisa(sText) { var reVisa = /^(4\d{12}("txt1"); if (isValidVisa(oInput1.value)) { alert("Valid"); } else { alert("Invalid!"); } } </script> </head> <body> <P>Visa Number: <input type="text" id="txt1" /><br /> <input type="button" value="Validate" onclick="validate()" /></p> </body> </html>
MasterCard验证:
<html> <head> <title>MasterCard Example</title> <script type="text/javascript"> function luhnCheckSum(sCardNum) { var iOddSum = 0; var iEvenSum = 0; var bIsOdd = true; for (var i=sCardNum.length-1; i >= 0; i--) { var iNum = parseInt(sCardNum.charAt(i)); if (bIsOdd) { iOddSum += iNum; } else { iNum = iNum * 2; if (iNum > 9) { iNum = eval(iNum.toString().split("").join("+")); } iEvenSum += iNum; } bIsOdd = !bIsOdd; } return ((iEvenSum + iOddSum) % 10 == 0); } function isValidMasterCard(sText) { var reMasterCard = /^(5[1-5]\d{2})[\s\-]"txt1"); if (isValidMasterCard(oInput1.value)) { alert("Valid"); } else { alert("Invalid!"); } } </script> </head> <body> <P>MasterCard Number: <input type="text" id="txt1" /><br /> <input type="button" value="Validate" onclick="validate()" /></p> </body> </html>
希望本文所述对大家的javascript程序设计有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“Javascript验证Visa和MasterCard信用卡号的方法”评论...