I am using the following code to display phone number in text field in the following format
and the format is 123-456-7890
and the code is working fine
and the code is as follows
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *numSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789-"];
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
int charCount = [newString length];
if ([string isEqualToString:@""])
{
return YES;
}
if ([newString rangeOfCharacterFromSet:[numSet invertedSet]].location != NSNotFound|| [string rangeOfString:@"-"].location != NSNotFound|| charCount > 12) {
return NO;
}
if (charCount == 3 || charCount == 7) {
newString = [newString stringByAppendingString:@"-"];
}
amountField.text = newString;
return NO;
}
I am using the UItextfield delegate method.
But when I am editing the text field up to "-" that mean (if i tried to change the number in text field 123-456-7890 it is not validating that mean 123-456 if i again enter the remaining number from here it is displaying as 123-4567890
can any one please help me how to validate this thing