i have a uitextfield and i am validating text field and the validation is adding “-“(hypen)for every 3 numbers. and it is working fine
and the code is as follows
but now i want to change the validation in such a way that to restrict no of characters to 10 and adding “-” after 3rd and 7th place dynamically while entering and editing
[ex:123-456-7890] it should be done while editing also…
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *separator = @"-";
int seperatorInterval = 3;
NSString *originalString = [textField.text stringByReplacingOccurrencesOfString:separator withString:@""];
if (![originalString isEqualToString:@""] && ![string isEqualToString:@""]) {
NSString *lastChar = [textField.text substringFromIndex:[textField.text length] - 1];
int modulus = [originalString length] % seperatorInterval;
if (![lastChar isEqualToString:separator] && modulus == 0) {
textField.text = [textField.text stringByAppendingString:separator];
}
}
return YES;
}
but now i want to change the validation in such a way that to restrict no of characters to 10 and adding “-” after 3rd and 7th place dynamically while entering and editing
[ex:123-456-7890]
it should be done while editing also…
You just need to add this block of code to the bottom of your function you have shown here.