Determine if the bank card number entered by the user is correct--a format check based on the Luhn algorithm

Source: Internet
Author: User

In development, sometimes, in order to create a better user experience, while reducing the pressure on the server, need for some such as, mobile phone number, bank card number, ID card number for the format check

Here is the code that determines if the bank card number is entered correctly (based on the Luhn algorithm's format check):

iOS code:

/**

* Bank card format check

*

* @param Cardno Bank card number

*

* @return

*/

+ (BOOL) Checkcardno: (nsstring*) cardno{

int oddsum = 0; Odd sum

int evensum = 0; Even sums

int allsum = 0;

int cardnolength = (int) [Cardno length];

int lastnum = [[Cardno substringfromindex:cardnolength-1] intvalue];

Cardno = [Cardno substringtoindex:cardnolength-1];

for (int i = cardNoLength-1; i>=1;i--) {

NSString *tmpstring = [Cardno substringwithrange:nsmakerange (I-1, 1)];

int tmpval = [tmpstring intvalue];

if (cardnolength% 2 ==1) {

if ((i% 2) = = 0) {

Tmpval *= 2;

if (tmpval>=10)

Tmpval-= 9;

Evensum + = Tmpval;

}else{

Oddsum + = Tmpval;

}

}else{

if ((i% 2) = = 1) {

Tmpval *= 2;

if (tmpval>=10)

Tmpval-= 9;

Evensum + = Tmpval;

}else{

Oddsum + = Tmpval;

}

}

}

Allsum = Oddsum + evensum;

Allsum + = Lastnum;

if ((allsum% 10) = = 0)

return YES;

Else

return NO;

}

Androd Code:

/**

* Matching Luhn algorithm: can be used to detect bank card number

* @param Cardno

* @return

*/

public static Boolean Matchluhn (String Cardno) {

int[] Cardnoarr = new int[cardno.length ()];

for (int i=0; i<cardno.length (); i++) {

Cardnoarr[i] = integer.valueof (string.valueof (Cardno.charat (i)));

}

for (int i=cardnoarr.length-2;i>=0;i-=2) {

Cardnoarr[i] <<= 1;

Cardnoarr[i] = CARDNOARR[I]/10 + cardnoarr[i]%10;

}

int sum = 0;

for (int i=0;i<cardnoarr.length;i++) {

Sum + = Cardnoarr[i];

}

return sum% 10 = = 0;

}

Attached (bank card number format):

In the process of entering the bank card number, we usually insert a "-" every 4 bits, similar to the format "1332-2131-2313-1231-212"
This can be done in the following ways:

Set up a proxy for TextField

-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *) string

{

NSString *text = [TextField text];

Nscharacterset *characterset = [Nscharacterset charactersetwithcharactersinstring:@ "0123456789\b"];

string = [String stringbyreplacingoccurrencesofstring:@ "-" withstring:@ ""];

if ([string rangeofcharacterfromset:[characterset invertedset]].location! = nsnotfound) {

return NO;

}

Text = [text Stringbyreplacingcharactersinrange:range withstring:string];

Text = [Text stringbyreplacingoccurrencesofstring:@ "-" withstring:@ ""];

NSString *newstring = @ "";

while (Text.length > 0) {

NSString *substring = [Text Substringtoindex:min (Text.length, 4)];

newstring = [NewString stringbyappendingstring:substring];

if (substring.length = = 4) {

newstring = [newstring stringbyappendingstring:@ "-"];

}

Text = [Text Substringfromindex:min (Text.length, 4)];

}

newstring = [newstring stringbytrimmingcharactersinset:[characterset Invertedset]];

if (newstring.length >= 24) {

return NO;

}

[TextField settext:newstring];

return NO;

}

Finally, how do I get a bank card number that does not contain the symbol "-"?

[Self.textField.text stringbyreplacingoccurrencesofstring:@ "-" withstring:@ ""]

Determine if the bank card number entered by the user is correct--a format check based on the Luhn algorithm

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.