I have a textfield
and I want the text to be entered in it in a format:
aa #### 1234
I know this should be done in shouldChangeCharactersInRange
delegate method of textfield
.nut I am not able to understand that how condition will be implemented on each character.Kindly give your suggestions.Thanks in advance!
Asked
Active
Viewed 88 times
2

Prez
- 227
- 3
- 12
-
1show your tried code – Anbu.Karthik Apr 26 '17 at 05:10
3 Answers
0
Create categories on UITextField and add this method write condition according your requirement. Below example for email id:
- (BOOL)validateRegEx:(NSString*)regexString
{
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexString
options:NSRegularExpressionCaseInsensitive
error:&error];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:self.text
options:0
range:NSMakeRange(0, [self.text length])];
return numberOfMatches > 0;
}

rmaddy
- 314,917
- 42
- 532
- 579

Wasim Akram
- 51
- 2
- 9
0
Try this
-(BOOL)checkString:(NSString *)str{
NSString *regex1 = @"^[a-bA-B0-9]"; // chabge regex as per your needs
NSPredicate *test1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex1];
return [test1 evaluateWithObject:str] ;
}

KKRocks
- 8,222
- 1
- 18
- 84
0
An expression that can be helpful to validate your needs might be:
^[a-z]{2}\s\d{4}\s\d{4}$
Here:
^
- Shows start of string[a-z]{2}
- Shows any character between a-z exactly 2 times\s
- Shows white space character one time.\d{4}
- Shows any digit from 0-9, exactly 4 times.$
- Shows end of string
An example as a workaround could be this, here the tricky part is to allow the under construction string:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSError *error = NULL;
NSRegularExpression *regex = nil;
NSMutableString *combinedText = [textField.text mutableCopy];
[combinedText replaceCharactersInRange:range withString:string];
switch (combinedText.length) {
case 1:
case 2:
regex = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@"^[a-z]{%ld}", (long)combinedText.length] options:NSRegularExpressionCaseInsensitive error:&error];
break;
case 3:
regex = [NSRegularExpression regularExpressionWithPattern:@"^[a-z]{2}\\s" options:NSRegularExpressionCaseInsensitive error:&error];
break;
case 4:
case 5:
case 6:
case 7:
regex = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@"^[a-z]{2}\\s\\d{%ld}", (long)combinedText.length - 3] options:NSRegularExpressionCaseInsensitive error:&error];
break;
case 8:
regex = [NSRegularExpression regularExpressionWithPattern:@"^[a-z]{2}\\s\\d{4}\\s" options:NSRegularExpressionCaseInsensitive error:&error];
break;
case 9:
case 10:
case 11:
case 12:
regex = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@"^[a-z]{2}\\s\\d{4}\\s\\d{%ld}$", (long)combinedText.length - 8] options:NSRegularExpressionCaseInsensitive error:&error];
break;
default:
return false;
}
if(error) return false;
NSUInteger numberOfMatches = [regex numberOfMatchesInString:combinedText options:0 range:NSMakeRange(0, combinedText.length)];
return numberOfMatches > 0 || string.length == 0;
}
Hope that helps!

NeverHopeless
- 11,077
- 4
- 35
- 56
-
@Prez, I have just checked that your content of question has been changed. Anyways, glad to see you made it. – NeverHopeless Apr 26 '17 at 07:24
-
Thanks Sir...I have achieved the complete thing but not able to understand why it crashes on backspace.Can you please provide some guidance for it? – Prez Apr 26 '17 at 07:46
-
My current solution support backspace handling with the help of `|| string.length == 0` and `range:NSMakeRange(0, combinedText.length)`. Do you have this in your code too ? – NeverHopeless Apr 26 '17 at 07:49