Format textfield while typing Ios
Posted
by
BBios
on Stack Overflow
See other posts from Stack Overflow
or by BBios
Published on 2012-06-26T15:05:56Z
Indexed on
2012/06/26
15:16 UTC
Read the original article
Hit count: 186
Ive seen other examples and tried
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
I am not sure what I am doing wrong, I just started coding my first Iphone App
This is what I am trying to do
I have 4 textfields and each has a limit on number of letters while typing I have done this using the below code
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
int valid;
NSString *cs2 = [textField.text stringByReplacingCharactersInRange:range withString:string];
// int charCount = [cs2 length];
if(textField == cvv){
valid = 4;
}else if(textField == cardName) {
valid=26;
}else if(textField == expDate) {
valid=5;
// if (charCount == 2 ) {
// textField.text = [cs2 stringByAppendingString:@"/"];
// textField.text = cs2;
// return YES;
// }
}else if(textField == acNumber) {
valid=19;
}
return !([cs2 length] > valid);
Works fine till here, I have a textfield where the user enters Exp date and would like to format it as if I am entering 112 then it should display as 01/12 and if I enter 2 then it should display as 1122
I tried checking if the length of the textfield value is 2 then append a / but then that gives me when I enter 12 it gives 11/22
© Stack Overflow or respective owner