判断银行卡号是否有效

+ (BOOL) isValidCreditNumber:(NSString*)value {
02. BOOL result = NO;
03. NSInteger length = [value length];
04. if (length >= 13) {
05. result = [WTCreditCard isValidNumber:value];
06. if (result)
07. {
08. NSInteger twoDigitBeginValue = [[value substringWithRange:NSMakeRange(0, 2)] integerValue];
09. //VISA
10. if([WTCreditCard isStartWith:value Str:@"4"]) {
11. if (13 == length||16 == length) {
12. result = TRUE;
13. }else {
14. result = NO;
15. }
16. }
17. //MasterCard
18. else if(twoDigitBeginValue >= 51 && twoDigitBeginValue <= 55 && length == 16) {
19. result = TRUE;
20. }
21. //American Express
22. else if(([WTCreditCard isStartWith:value Str:@"34"]||[WTCreditCard isStartWith:value Str:@"37"]) && length == 15){
23. result = TRUE;
24. }
25. //Discover
26. else if([WTCreditCard isStartWith:value Str:@"6011"] && length == 16) {
27. result = TRUE;
28. }else {
29. result = FALSE;
30. }
31. }
32. if (result)
33. {
34. NSInteger digitValue;
35. NSInteger checkSum = 0;
36. NSInteger index = 0;
37. NSInteger leftIndex;
38. //even length, odd index
39. if (0 == length%2) {
40. index = 0;
41. leftIndex = 1;
42. }
43. //odd length, even index
44. else {
45. index = 1;
46. leftIndex = 0;
47. }
48. while (index < length) {
49. digitValue = [[value substringWithRange:NSMakeRange(index, 1)] integerValue];
50. digitValue = digitValue*2;
51. if (digitValue >= 10)
52. {
53. checkSum += digitValue/10 + digitValue%10;
54. }
55. else
56. {
57. checkSum += digitValue;
58. }
59. digitValue = [[value substringWithRange:NSMakeRange(leftIndex, 1)] integerValue];
60. checkSum += digitValue;
61. index += 2;
62. leftIndex += 2;
63. }
64. result = (0 == checkSum%10) ? TRUE:FALSE;
65. }
66. }else {
67. result = NO;
68. }
69. return result;
70. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值