2

I am trying to add - after use enter for characters in UITextField. The situation is adding a credit/debit card number. I've searched around but the methods are not valid as per my knowledge. I've set the limit to 19 characters that is 16 card numbers and 3 - in the delegate method as:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger length = [[textField text] length] - range.length + string.length;
return textField.text.length <=19;
}

So now the length is giving me the exact length if UITextField at that time. Which is working fine now I need to know what should I write if this field reaches 3, 7 or 11 add - in the field. All the cards that will be entered are in this format xxxx-xxxx-xxxx-xxxx so that is what I'm trying to do adding - after 4 characters.
I've also tried this inside delegate method but it didn't work:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        [formatter setGroupingSeparator:@"-"];
        [formatter setGroupingSize:4];
        [formatter setUsesGroupingSeparator:YES];

        NSString *num = textField.text ;
        num= [num stringByReplacingOccurrencesOfString:@"" withString:@"-"];
       NSString *str = [formatter stringFromNumber:[NSNumber numberWithDouble:[num doubleValue]]];
        textField.text=str;
        NSLog(@"%@",str);

        return textField.text.length <=19;
Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
Chaudhry Talha
  • 7,231
  • 11
  • 67
  • 116
  • Have you checked this .. http://stackoverflow.com/questions/12083605/formatting-a-uitextfield-for-credit-card-input-like-xxxx-xxxx-xxxx-xxxx – dev_m Jan 20 '16 at 07:13

3 Answers3

1

Try this in your delegate method this is another approach

if (textField.text.length < 19 && ![string isEqualToString:@""]) {
    NSString *tempoText = textField.text;
    tempoText = [tempoText stringByReplacingOccurrencesOfString:@"-" withString:@""];

    if (tempoText.length >= 4) {
        NSMutableString *mutString = [tempoText mutableCopy];
        NSUInteger len = mutString.length / 4;

        int j = 0;
        for (int i = 1; i <= len; i ++) {

            NSUInteger index = 4 * i;
            index += j;
            j++;
            [mutString insertString:@"-" atIndex:index];

        }

        tempoText = mutString;
    }

    [textField setText:tempoText];
    return YES;
}
Mohamad Sheikh
  • 283
  • 2
  • 7
0

https://github.com/stripe/PaymentKit/tree/master/PaymentKit

use this third party library for credit/debit card it's a batter and simply use.

Akshay Patel
  • 157
  • 5
0

Try this it will work

   - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init] ; 
    if([string length]==0)
    {
        [formatter setGroupingSeparator:@"-"];
        [formatter setGroupingSize:4];
        [formatter setUsesGroupingSeparator:YES];
        [formatter setSecondaryGroupingSize:2];
        NSString *num = textField.text ;
        num= [num stringByReplacingOccurrencesOfString:@"-" withString:@""];
        NSString *str = [formatter stringFromNumber:[NSNumber numberWithDouble:[num doubleValue]]];
        [formatter release];
        textField.text=str;
        NSLog(@"%@",str);
        return YES;
    }
    else {
        [formatter setGroupingSeparator:@"-"];
        [formatter setGroupingSize:2];
        [formatter setUsesGroupingSeparator:YES];
        [formatter setSecondaryGroupingSize:2];
        NSString *num = textField.text ;
        if(![num isEqualToString:@""])
        {
            num= [num stringByReplacingOccurrencesOfString:@"-" withString:@""];
            NSString *str = [formatter stringFromNumber:[NSNumber numberWithDouble:[num doubleValue]]];
            [formatter release];
            textField.text=str;
        }

        //NSLog(@"%@",str);
        return YES;
    }

    //[formatter setLenient:YES];

}
viratpuar
  • 524
  • 1
  • 5
  • 22
  • 1
    it's working but there are some issues like user can enter more than `16` characters which I encountered by returning `<=15` but still on the last character it cause some problems like user cannot delete the characters and some other abnormal code behaviour. – Chaudhry Talha Jan 20 '16 at 07:40