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; }