Bank card number Split input

Source: Internet
Author: User

1. Create two global variables to comply with Uitextfield agents

NSString *_previoustextfieldcontent;

Uitextrange *_previousselection;

2. Add an event setting agent to the TextField of the input bank card number

    _bankcardnumtextfield.delegate=self

[_bankcardnumtextfield addtarget:self Action: @selector (Reformatasbankcardnumber:) forControlEvents: Uicontroleventeditingchanged];

3. Paste the following code in your. m file

#pragma mark-reformat the bank card number

-(void) Reformatasbankcardnumber: (Uitextfield *) TextField

{

//Determine the correct cursor position

Nsuinteger targetcursorpostion = [TextField offsetFromPosition:textField.beginningOfDocument toposition: TextField.selectedTextRange.start];

No bank card number with spaces inserted

NSString *bankcardnumberwithoutspaces = [self RemoveNonDigits:textField.text andpreservecursorposition:& Targetcursorpostion];

Avoid more than 19 bits of input

if (Bankcardnumberwithoutspaces.length > 19) {

[TextField settext:_previoustextfieldcontent];

Textfield.selectedtextrange = _previousselection;

Return

}

Get the bank card number after inserting a space

NSString *bankcardnumberwithspaces = [Self insertspaceseveryfourdigitsintostring:bankcardnumberwithoutspaces andpreservecursorposition:&targetcursorpostion];

Textfield.text = bankcardnumberwithspaces;

Uitextposition *targetpostion = [TextField positionFromPosition:textField.beginningOfDocument offset: Targetcursorpostion];

[TextField Setselectedtextrange:[textfield textrangefromposition:targetpostion toposition:targetpostion];

 }

#pragma mark-Remove non-numeric characters (spaces) to determine the correct position of the cursor

String: Current string cursorposition: string after return processing of the cursor position

-(NSString *) Removenondigits: (NSString *) string andpreservecursorposition: (Nsuinteger *) Cursorposition

{

Nsuinteger originalcursorposition =*cursorposition;

nsmutablestring *digitsonlystring = [nsmutablestring new];

for (Nsuinteger i=0; i<string.length; i++) {

Unichar Charactertoadd = [string characteratindex:i];

if (IsDigit (Charactertoadd)) {

NSString *stringtoadd = [NSString stringwithcharacters:&charactertoadd length:1];

[Digitsonlystring Appendstring:stringtoadd];

}

else {

if (i<originalcursorposition) {

(*cursorposition)--;

}

}

}

return digitsonlystring;

}

#pragma mark-Inserts a space into our current string and determines the correct position of the cursor to prevent problems in the space

String: Current string cursorposition: Cursor position return: string with space after processing

-(NSString *) insertspaceseveryfourdigitsintostring: (NSString *) string andpreservecursorposition: (NSUInteger *) Cursorposition

{

nsmutablestring *stringwithaddedspaces = [nsmutablestring new];

Nsuinteger cursorpositioninspacelessstring = *cursorposition;

for (Nsuinteger i=0; i<string.length; i++) {

if (i > 0)

{

if (i%4 = = 0) {

[Stringwithaddedspaces appendstring:@ ""];

if (I < cursorpositioninspacelessstring) {

(*cursorposition) + +;

}

}

}

Unichar Charactertoadd = [string characteratindex:i];

NSString *stringtoadd = [NSString stringwithcharacters:&charactertoadd length:1];

[Stringwithaddedspaces Appendstring:stringtoadd];

}

return stringwithaddedspaces;

}

#pragma mark-uitextfielddelegate

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

{

_previousselection = Textfield.selectedtextrange;

_previoustextfieldcontent = Textfield.text;

return YES;

}

Bank card number Split input

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.