ChinaUnionPay:
1 <html> 2 3 <head> 4 <title> 5 ChinaUnionPay Example 6 </title> 7 <script language="javascript" type="text/javascript"> 8 // 验证信用卡或银联卡卡号是否符合标准, 9 // 如果符合返回true,否则返回false. 10 function validateCard(number) { 11 try { 12 if (number.length == 0 || number.length < 12 || number.length > 19) return false; 13 var Exp = /[34569]/; 14 var objExp = new RegExp(Exp); 15 if (objExp.test(number.charAt(0) == false)) return false; 16 var tempNumber = reverse(number); //反转 17 var tempS = 0; //保存基数之和 18 var tempD = 0; //保存偶数位*2后之和 19 for (var i = 0; i < tempNumber.length; i++) { 20 if (i % 2 == 0) //说明是基数,因为从0位开始 21 { 22 tempS = tempS + tempNumber.charAt(i) * 1; 23 } else { 24 var temp = tempNumber.charAt(i) * 2; 25 if (temp > 9) { 26 tempD = tempD + (temp / 10 | 0) + temp % 10; 27 } else tempD = tempD + temp; 28 } 29 } 30 var sum = tempS + tempD; 31 if (sum % 10 == 0) return true; 32 else return false; 33 } catch(ex) { 34 return false; 35 } 36 37 } 38 39 // @param {Object} source,被反转的字符串,返回反转后的字符串 40 function reverse(source) { 41 var s = source; 42 var ss = ""; 43 for (i = s.length - 1; i >= 0; i--) { 44 ss = ss + s.charAt(i); 45 } 46 return ss; 47 } 48 49 function validate() { 50 var oInput1 = document.getElementById("txt1"); 51 if (validateCard(oInput1.value)) { 52 alert("Valid"); 53 } else { 54 alert("Invalid!"); 55 } 56 57 } 58 </script> 59 </head> 60 61 <body> 62 <p> 63 ChinaUnionPay Number: 64 <input type="text" id="txt1" /> 65 <br /> 66 <input type="button" value="Validate" onclick="validate()" /> 67 </p> 68 </body> 69 70 </html>
Visa:
1 <html> 2 3 <head> 4 <title> 5 Visa Example 6 </title> 7 <script type="text/javascript"> 8 function luhnCheckSum(sCardNum) { 9 10 var iOddSum = 0; 11 var iEvenSum = 0; 12 var bIsOdd = true; 13 14 for (var i = sCardNum.length - 1; i >= 0; i--) { 15 16 var iNum = parseInt(sCardNum.charAt(i)); 17 18 if (bIsOdd) { 19 iOddSum += iNum; 20 } else { 21 iNum = iNum * 2; 22 if (iNum > 9) { 23 iNum = eval(iNum.toString().split("").join("+")); 24 } 25 iEvenSum += iNum; 26 } 27 28 bIsOdd = !bIsOdd; 29 } 30 31 return ((iEvenSum + iOddSum) % 10 == 0); 32 } 33 34 function isValidVisa(sText) { 35 var reVisa = /^(4\d{12}(?:\d{3})?)$/; 36 37 if (reVisa.test(sText)) { 38 return luhnCheckSum(RegExp.$1); 39 } else { 40 return false; 41 } 42 } 43 44 function validate() { 45 var oInput1 = document.getElementById("txt1"); 46 if (isValidVisa(oInput1.value)) { 47 alert("Valid"); 48 } else { 49 alert("Invalid!"); 50 } 51 52 } 53 </script> 54 </head> 55 56 <body> 57 <P> 58 Visa Number: 59 <input type="text" id="txt1" /> 60 <br /> 61 <input type="button" value="Validate" onclick="validate()" /> 62 </p> 63 </body> 64 65 </html>
MasterCard:
1 <html> 2 3 <head> 4 <title> 5 MasterCard Example 6 </title> 7 <script type="text/javascript"> 8 function luhnCheckSum(sCardNum) { 9 10 var iOddSum = 0; 11 var iEvenSum = 0; 12 var bIsOdd = true; 13 14 for (var i = sCardNum.length - 1; i >= 0; i--) { 15 16 var iNum = parseInt(sCardNum.charAt(i)); 17 18 if (bIsOdd) { 19 iOddSum += iNum; 20 } else { 21 iNum = iNum * 2; 22 if (iNum > 9) { 23 iNum = eval(iNum.toString().split("").join("+")); 24 } 25 iEvenSum += iNum; 26 } 27 28 bIsOdd = !bIsOdd; 29 } 30 31 return ((iEvenSum + iOddSum) % 10 == 0); 32 } 33 34 function isValidMasterCard(sText) { 35 var reMasterCard = /^(5[1-5]\d{2})[\s\-]?(\d{4})[\s\-]?(\d{4})[\s\-]?(\d{4})$/; 36 37 if (reMasterCard.test(sText)) { 38 39 var sCardNum = RegExp.$1 + RegExp.$2 + RegExp.$3 + RegExp.$4; 40 41 return luhnCheckSum(sCardNum); 42 43 } else { 44 return false; 45 } 46 } 47 48 function validate() { 49 var oInput1 = document.getElementById("txt1"); 50 if (isValidMasterCard(oInput1.value)) { 51 alert("Valid"); 52 } else { 53 alert("Invalid!"); 54 } 55 56 } 57 </script> 58 </head> 59 60 <body> 61 <P> 62 MasterCard Number: 63 <input type="text" id="txt1" /> 64 <br /> 65 <input type="button" value="Validate" onclick="validate()" /> 66 </p> 67 </body> 68 69 </html>