iOS determines whether the bank card number entered by the user is correct

Source: Internet
Author: User
Tags vars

In order to create a better user experience, while reducing the pressure on the server side, for some such as, mobile phone number, bank card number, ID number to determine whether the correct is very necessary

Here is a small paragraph to determine whether the bank card number input is correct code method for your reference

  1. -(void) Viewdidload {
  2. [Super Viewdidload];
  3. NSString *str = @ "6226820011200783033";
  4. BOOL isright = [self checkcardno:str];
  5. if (!isright) {
  6. Uialertview *alert = [[Uialertview Alloc]initwithtitle:@ "not" message:@ "Please reenter card number" delegate: Nil  cancelbuttontitle: nil otherbuttontitles: nil, nil nil];
  7. [Alert Show];
  8. }else{
  9. Uialertview *alert = [[Uialertview Alloc]initwithtitle:@ ' to ' message:@ ' delegate: Nil   Cancelbuttontitle: Nil otherbuttontitles: nil, nil nil];
  10. [Alert show];}
  11. }

Method ******///

  1. -(BOOL) Checkcardno: (nsstring*) cardno{
  2. int oddsum = 0; //Odd sum
  3. int evensum = 0; //Even sum
  4. int allsum = 0;
  5. int cardnolength = (int) [Cardno length];
  6. int lastnum = [[Cardno substringfromindex:cardnolength-1] intvalue];
  7. Cardno = [Cardno substringtoindex:cardnolength- 1];
  8. For (int i = cardnolength-1; i>=1;i--) {
  9. nsstring *tmpstring = [Cardno substringwithrange:nsmakerange (i-1, 1)];
  10. int tmpval = [tmpstring intvalue];
  11. if (cardnolength% 2 = =1) {
  12. if ((i% 2) = = 0) {
  13. tmpval *= 2;
  14. if (tmpval>=10)
  15. Tmpval-= 9;
  16. Evensum + = Tmpval;
  17. }else{
  18. Oddsum + = Tmpval;
  19. }
  20. }else{
  21. if ((i% 2) = = 1) {
  22. tmpval *= 2;
  23. if (tmpval>=10)
  24. Tmpval-= 9;
  25. Evensum + = Tmpval;
  26. }else{
  27. Oddsum + = Tmpval;
  28. }
  29. }
  30. }
  31. Allsum = Oddsum + evensum;
  32. Allsum + = Lastnum;
  33. if ((allsum% 10) = = 0)
  34. return YES;
  35. Else
  36. return NO;
  37. }

iOS determines whether the bank card number entered by the user is correct

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.