Solutions for using Uitextfield to format bank card numbers in IOS _ios

Source: Internet
Author: User
Tags delete key

Do format the bank card today, to avoid the repetition of the wheel, find the Niang check, see a good way to achieve, recorded, and with the implementation of ideas

Call-(BOOL) TextField after #pragma mark-uitextfielddelegate Uitextfield type characters: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) Range replacementstring: (NSString *) string {//Get the string before the change nsstring = [
 TextField text];
 Type character set, \b Mark Delete key nscharacterset *characterset = [Nscharacterset charactersetwithcharactersinstring:@ "0123456789\b"];
 Filters the current typed characters by a space filter string = [string stringbyreplacingoccurrencesofstring:@ ' withstring:@ ']; Invertedset will reverse the current result set, checking whether the currently typed character is in the character set, or returning no without changing the TextField value if ([String rangeofcharacterfromset:[
 CharacterSet invertedset]].location!= nsnotfound) {return NO;
 //Add the string trailing text = [text Stringbyreplacingcharactersinrange:range withstring:string] before the change of the current type character;
 Again confirm remove the string hollow Text = [text stringbyreplacingoccurrencesofstring:@ "" withstring:@ "];
 The initialization character is used to save the formatted string nsstring *newstring = @ ""; While the text is formatted while (Text.length > 0) {//intercepted by 4-bit characters, if the current character is less than 4 bits, the NSString *substring is intercepted according to the maximum length of the current string = [text Sub Stringtoindex:min (TEXT.LEngth, 4)];
  Place the intercepted character in a string that needs to be formatted newstring = [NewString stringbyappendingstring:substring];
  if (substring.length = = 4) {//intercepted string length full 4 digits then add a spaces at the back newstring = [newstring stringbyappendingstring:@ "];
 ///Remove the string from text = [Text Substringfromindex:min (Text.length, 4)];
 //Confirm again to filter out characters other than the specified character newstring = [newstring stringbytrimmingcharactersinset:[characterset Invertedset]];
 Domestic bank cards are generally 16~19-bit format added 4 spaces is the maximum of 23 characters if (Newstring.length >) {return NO;
 ///Manually assign value to TextField [TextField settext:newstring];
Return no does not automatically add characters to the current character after the delegate, the formatting effect returns no; }

Related Article

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.