Visual Studio .NET 2002Visual Studio .NET 2003WebFormsVisual Studio 2005初学者开发Visual StudioWindows.NETASP.NETC#
客户端和服务器端信用卡验证...(我的简单方法系列文章)






2.69/5 (14投票s)
简单的客户端和服务器端信用卡验证
引言
使用正则表达式进行信用卡验证。
背景
你需要具备正则表达式的一些知识,因为这是此功能的核心部分。
使用代码
不用担心,代码很简单,你只要看一眼就能理解...// // first Client side validation using JavaScript... // you have to put to controls one is drop down list (for different credit //cards names)and other is textbox (for card number). //just pass two parameter to this function one is id of dropdownlist and //textbox. function ValidateCC(CCType, CCNum) { var cctype= document.getElementById(CCType); var ccnum= document.getElementById(CCNum); var validCCNum=false; var validCC=false; if(ccnum.value == "") { return false; } validCC= isValidCreditCard (cctype.options[cctype.selectedIndex].value,ccnum.value); if( validCC) { return true; } return false; } // this function is calling another function isValidCreditCard //for number validation and it is here... function isValidCreditCard(type, ccnum) { if (type == "Visa") var re = /^[4]([0-9]{15}$|[0-9]{12}$)/; else if (type == "MasterCard") var re = /^[5][1-5][0-9]{14}$/; else if (type == "Discover") var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/; else if (type == "Diners Club") var re = /(^30[0-5][0-9]{11}$)|(^(36|38)[0-9]{12}$)/; else if (type == "American Express") var re = /^[34|37][0-9]{14}$/; else if (type == "enRoute") var re = /^(2014|2149)[0-9]{11}$/; else if (type == "JCB") var re = /(^3[0-9]{15}$)|(^(2131|1800)[0-9]{11}$)/; if (!re.test(ccnum)) return false; ccnum = ccnum.split("-").join(""); var checksum = 0; for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) { checksum += parseInt(ccnum.charAt(i-1)); } for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) { var digit = parseInt(ccnum.charAt(i-1)) * 2; if (digit < 10) { checksum += digit; } else { checksum += (digit-9);} } if ((checksum % 10) == 0) { return true; } else return false; } //now at the server side in asp.net with c#... private bool checkCCValidation() { bool validCC = false; if(txtCCNumber.Text == "") { return false; } validCC= isValidCreditCard(selectCCType.Value,txtCCNumber.Text.Trim()); if( validCC) { return true; } return false; } //this method is also calling another method and it is here.. private bool isValidCreditCard(string type, string ccnum) { string regExp = ""; if (type == "Visa") regExp = "^[4]([0-9]{15}$|[0-9]{12}$)"; else if (type == "MasterCard") regExp = "^[5][1-5][0-9]{14}$"; else if (type == "Discover") regExp = "^6011-?\\d{4}-?\\d{4}-?\\d{4}$"; else if (type == "Diners Club") regExp = "(^30[0-5][0-9]{11}$)|(^(36|38)[0-9]{12}$)"; else if (type == "American Express") regExp = "^[34|37][0-9]{14}$"; else if (type == "enRoute") regExp = "^(2014|2149)[0-9]{11}$"; else if (type == "JCB") regExp = "(^3[0-9]{15}$)|(^(2131|1800)[0-9]{11}$)"; if (!Regex.IsMatch(ccnum,regExp)) return false; string[] tempNo = ccnum.Split('-'); ccnum = String.Join("", tempNo); int checksum = 0; for (int i = (2-(ccnum.Length % 2)); i <= ccnum.Length; i += 2) { checksum += Convert.ToInt32(ccnum[i-1].ToString()); } int digit = 0; for (int i = (ccnum.Length % 2) + 1; i < ccnum.Length; i += 2) { digit = 0; digit = Convert.ToInt32(ccnum[i-1].ToString()) * 2; if (digit < 10) { checksum += digit; } else { checksum += (digit - 9); } } if ((checksum % 10) == 0) return true; else return false; } //so that is the end of simple way of validating credit card, without using //any third party controls..so enjoy...
请记得使用语言下拉菜单设置代码片段的语言。
使用“var”按钮将变量或类名包裹在标签中,例如
this
。
关注点
我喜欢与像您这样的人分享我的知识,因为我也是你们中的一员,如果我不知道任何事情,我会来找您...所以保持知识的交流...
历史
请继续关注我的简单方法系列文章....