TextField Validation With Regular Expression - iphone

I need help with code that looks at a textfield make sure it starts with either a (+ or -) then has 3 integers after it.
So valid data looks like +234 or -888
So I have started this code but there are 2 problems with it
It correctly validates that only 4 characters are entered. But for some reason you have to take focus off the textfield in order for the Done button on the keyboard to fire and hide the keyboard. If I only put less than 4 characters in the textfield then the Done button works fine. But I dont want the user to enter anything but 4 characters and then press Done and hide the keyboard. Thats the first problem....
I am not familar with regular expressions and how to use them in iphone. So I need to add to this code regular expression for the above requirement.
-(BOOL)textField:(UITextField*)textFieldshouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
return !([newString length] > 4);
}
//Done button to hide the keyboard
-(IBAction)Done:(id)sender
{
}

I am not sure how you'd like to handle user input and feedback. First I'll show a simple way to keep the user in the editing mode of the textField if her input is not valid.
First of all two delegate methods:
- (BOOL)textFieldShouldReturn:(UITextField *)aTextField
{
[aTextField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)aTextField
{
return [self validateInputWithString:aTextField.text];
}
The testing method, which just returns YES or NO whether the input is valid or not:
- (BOOL)validateInputWithString:(NSString *)aString
{
NSString * const regularExpression = #"^([+-]{1})([0-9]{3})$";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regularExpression
options:NSRegularExpressionCaseInsensitive
error:&error];
if (error) {
NSLog(#"error %#", error);
}
NSUInteger numberOfMatches = [regex numberOfMatchesInString:aString
options:0
range:NSMakeRange(0, [aString length])];
return numberOfMatches > 0;
}
That's it. However I'd recommend showing some live status to the user whether his input is ok or not. Add the following notifcation, for example in your viewDidLoad method:
- (void)viewDidLoad
{
// ...
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(validateInputCallback:)
name:#"UITextFieldTextDidChangeNotification"
object:nil];
}
- (void)validateInputCallback:(id)sender
{
if ([self validateInputWithString:textField.text]) {
// For example turn a label green and let it say: "OK"
} else {
// For example turn a label red and let it say: "Allowed: + or minus followed by exactly three digits"
}
}
Finally: If you need to access the capture groups (+ or - and the number) of the regular expression the following code will help:
// ... reg ex creation ...
NSArray *matches = [regex matchesInString:aString
options:0
range:NSMakeRange(0, [aString length])];
for (NSTextCheckingResult *match in matches) {
for (int i = 0; i < [match numberOfRanges]; i++) {
NSLog(#"range %d: %d %d", i, [match rangeAtIndex:i].location, [match rangeAtIndex:i].length);
NSLog(#"substring %d: %#", i, [aString substringWithRange:[match rangeAtIndex:i]]);
}
}

Validate Email id or Phone number using Regular Expression
Ddelegate methods:
- (BOOL)textFieldShouldReturn:(UITextField *)aTextField
{
[aTextField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)aTextField
{
return [self validateEmail:aTextField.text]; // Change validateEmail to validatePhone for phone validation.
}
Returns YES or NO whether the input is valid or not:
- (BOOL) validateEmail: (NSString *) candidate {
NSString *emailRegex = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
return [emailTest evaluateWithObject:candidate];
}
- (BOOL) validatePhone: (NSString *) candidate {
NSString *phoneRegex = #"^+(?:[0-9] ?){6,14}[0-9]$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", phoneRegex];
return [phoneTest evaluateWithObject:candidate];
}

Use UIPickerView instead
I am departing from the question title but IMHO considering what you need, it may be better to use UIPickerView to have a "spinning-wheel" type of entry, like you do in the Clock app when setting alarm. It can start at "+000" and user can tumble some of the four wheels ([+-]. [0-9], [0-9], [0-9]) as needed

Implement the -textField:shouldChangeCharactersInRange:replacementString in your UITextFieldDelegate and use NSRegularExpression to validate the changes.

Related

Price ui text field: how to allow user to enter only one decimal dot in to the text field for price like 283.99 [duplicate]

This question already has answers here:
Limiting text field entry to only one decimal point
(10 answers)
Closed 9 years ago.
I have tried with this code as follow
this helps me to allow user to enter only numbers and dot (decimal point)
But the problem is user can allow n number of decimals in this method.
I want to allow only one decimal
and only two digits after the decima
like 123.00 , 123423432353.99
but not like 123.4.4 , 123.12345, 123...23
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (string.length == 0) {
return YES;
}
NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:#"0123456789."];
for (int i = 0; i < [string length]; i++) {
unichar c = [string characterAtIndex:i];
if ([myCharSet characterIsMember:c]) {
return YES;
}
}
UIAlertView *av = [[UIAlertView alloc] initWithTitle:nil message:#"Invalid input" delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[av show];
return NO;
}
How to allow user to enter only one decimal the text field that too allow only two digits after the decimal
thanks in advance
Best practices Use RegularExpressions whenever you have to perform any string format validation like Email,Phone Number,Currency etc.
This surely will solve your problem. Here sample code below:
First create instance of NSRegularExpression
NSError error;
NSRegularExpression * regExp = [[NSRegularExpression alloc]initWithPattern:#"^\\d{0,10}(([.]\\d{1,2})|([.]))?$" options:NSRegularExpressionCaseInsensitive error:&error];
then use in your relevant method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString * existingText = textField.text;
NSString * completeText = [existingText stringByAppendingFormat:#"%#",string];
if ([regExp numberOfMatchesInString:completeText options:0 range:NSMakeRange(0, [completeText length])])
{
if ([completeText isEqualToString:#"."])
[textField insertText:#"0"];
return YES;
}
else
return NO;
}
Use and let me know if it works.
Please try to use this one...It may helps you and please implement your functionality.This code for only 2 digit after "."
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSArray *sep = [newString componentsSeparatedByString:#"."];
if([sep count]>=2)
{
NSString *sepStr=[NSString stringWithFormat:#"%#",[sep objectAtIndex:1]];
return !([sepStr length]>2);
}
return YES;

Setting validations on textfields

I m doing a registration form and if any of the textfield is left empty then I want to validate it .But I dont want to display a regular UIAlertView when textfield is empty.
Can I display any redcoloured star marked or anything else beside particular textField?
How can I validate to check for emailId pattern?
You can set text with red color in your textField
if ([yourTextField.text isEqualToString:#""])
{
yourTextField.textColor=[UIColor redColor];
yourTextField.text=#"Value Required";
}
set delegate of your textField in .h file
<UITextFieldDelegate>
and in textFieldDidBeginEditing you can change your textField color
If you are setting * in your textField then do this
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if ([textField.text isEqualToString:#"*"])
{
textField.text=#"";
textField.textColor=[UIColor blackColor];
}
}
Or you can make a label beside your textField and hide it initially and show when ever you needed.
Validation for email
- (BOOL)isValidEmailId:(NSString*)email
{
NSString *emailRegex = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
return [emailTest evaluateWithObject:email];
}
Validation for email field:
- (BOOL)EmailValidationL:(NSString *)email
{
NSString *emailRegEx =#"(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"
#"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
#"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")#(?:(?:[a-z0-9](?:[a-"
#"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5"
#"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"
#"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"
#"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
NSPredicate *regExPredicate = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegEx];
a= [regExPredicate evaluateWithObject:email];
return a;
}
And for checking it do like below:
if (![self EmailValidationL:emailtxt.text])
{
mailAlert =[[UIAlertView alloc]initWithTitle:#"Error"
message:#"Email Field Is Not Valid"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:#"OK",nil];
[mailAlert show];
[mailAlert release];
}
Yes you can show a red coloured image or label with * text on it and you can hide and show it like wise .
To validate email you can use the following code
+(BOOL) validateEmail: (NSString *) email
{
NSString *emailRegex = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
BOOL isValid = [emailTest evaluateWithObject:email];
return isValid;
}
You can try this:
First, take simple label in XIB with simple text & color:
-(void)DidLoad
{
-----
lbl.hidden = YES;
-----
}
Attributes:
text = "* email invalid"
color = "red"
Or whatever you like.
'textDidEndEditing' method.
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if(textField == txtEmail)
{
if(![txtEmail.text isEqualToString:#""])
{
bool chk = [self validateEmail:txtEmail.text];
if(!chk)
{
lbl.hidden = NO;
lbl.text = #"email invalid";
}
else
lbl.hidden = YES;
}
else
{
lbl.hidden = NO;
lbl.text = #"* required";
}
}
}
Email Validation
+(BOOL) validateEmail: (NSString *) email
{
NSString *emailRegex = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
BOOL isValid = [emailTest evaluateWithObject:email];
return isValid;
}
Ok so you get many solution for Email.
so lets go for star. You may have many way for this let me give two of them
Add hidden UIImageView where you want to show start and make is visible when use left it empty
use [UIView addSubView:UIImageView] to show start when you want to show.
If u want to show normal * in red color you can use UILabel instead of UIImageView.
I use following to validate email:
NSString *emailRegex = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
if(![emailTest evaluateWithObject:txtMail.text])
{
//Your code if wrong email
}
All the best....
Firstly Shashank Kulshrestha has given nice answer of validating email . Now if you want show some thing for indicating the invalid or empty input do following to that text field. It will show red edges.
1.Add QuartzCore framework.
2.Import #import
3.add following code :
if (textfield==empty || invalid)
{
textField.layer.cornerRadius=8.0f;
textField.layer.masksToBounds=YES;
textField.layer.borderColor=[[UIColor redColor]CGColor];
textField.layer.borderWidth= 1.0f;
}

Suggest # tags while typing (like Twitter) for iPhone UITextView

I'd building an app that uses hashtags, like Twitter or Tweetbot. When you're typing a message, if you type the hashtag symbol, I'd like to suggest tags that match the current one you're typing.
I've already figured out how to get the UITableView to appear and show a list of hashtags, but what I can't figure out is how to do the following:
Get the NSRange of the current word being typed,
See if that range is formatted like a hashtag (NSRegularExpression #"#\\w\\w*")
(From here on out, I've got the code figured out to search for matching hashtags and show them in the UITableView)
Can anyone help me with steps 1 and 2? I've been thinking about using textViewDidChange:, but I'm concerned that the app's performance might suffer if I'm constantly running methods every time the characters change.
Thanks!
I figured it out! I wound up using the textViewDidChange: and textViewDidChangeSelection: methods.
To get the NSRange of the current hashtag being typed, I ran a for loop over the NSRegularExpression matches in the text string. From there, I used NSLocationInRange to find out if the current cursor position intersected any of the hashtags.
Here's the code:
//Get the ranges of current hashtags
NSArray *hashtagRanges = [StringChecker rangesOfHashtagsInString:textView.text];
NSTextCheckingResult *currentHashtag;
if ([hashtagRanges count] >0)
{
//List the ranges of all the hashtags
for (int i = 0; i<[hashtagRanges count]; i++)
{
NSTextCheckingResult *hashtag = [hashtagRanges objectAtIndex:i];
//Check if the currentRange intersects the hashtag
//Have to add an extra space to the range for if you're at the end of a hashtag. (since NSLocationInRange uses a < instead of <=)
NSRange currentlyTypingHashtagRange = NSMakeRange(hashtag.range.location, hashtag.range.length + 1);
if (NSLocationInRange(currentRange.location, currentlyTypingHashtagRange))
{
//If the cursor is over the hashtag, then snag that hashtag for matching purposes.
currentHashtag = hashtag;
}
}
if (currentHashtag){
//If we found one hashtag that we're currently editing
//Display the hashtag suggester, feed it the current hashtag for matching.
[self showTagTable];
//Get the current list of hashtags into an array
NSFetchRequest *hashtagRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *tagEntityDescription = [NSEntityDescription entityForName:#"Tags"
inManagedObjectContext:self.note.managedObjectContext];
[hashtagRequest setEntity:tagEntityDescription];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"dateLastUsed"
ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[hashtagRequest setSortDescriptors:sortDescriptors];
NSPredicate *tagPredicate = [NSPredicate predicateWithFormat:#"name contains[c] %#", [noteTextView.text substringWithRange:currentHashtag.range]];
[hashtagRequest setPredicate:tagPredicate];
tagsToDisplay = (NSMutableArray *)[self.note.managedObjectContext executeFetchRequest:hashtagRequest error:nil];
[tagListTable reloadData];
//If there are no matching hashtags, then let's hide the tag table.
if ([tagsToDisplay count] == 0)
{
[self hideTagTable];
return;
}
}
The StringChecker class is a custom one that I wrote, it just has class methods that parse the strings. I made StringChecker a class because the methods are used in several places in the app. Here's the method:
#pragma mark - Hashtag Methods
+(NSArray *)rangesOfHashtagsInString:(NSString *)string {
NSRegularExpression *hashtagDetector = [[NSRegularExpression alloc] initWithPattern:#"#\\w\\w*"
options:NSRegularExpressionCaseInsensitive
error:nil];
NSArray *hashtagRanges = [hashtagDetector matchesInString:string
options:NSMatchingWithoutAnchoringBounds
range:NSMakeRange(0, string.length)];
return hashtagRanges;
}
+(NSUInteger)numberOfHashtagsInString:(NSString *)string {
NSRegularExpression *hashtagDetector = [[NSRegularExpression alloc] initWithPattern:#"#\\w\\w*"
options:NSRegularExpressionCaseInsensitive
error:nil];
NSUInteger numberOfHashtags = [hashtagDetector numberOfMatchesInString:string
options:NSRegularExpressionCaseInsensitive
range:NSMakeRange(0, string.length)];
return numberOfHashtags;
}
Another way I figured out to do this is as follows.
In the - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text function I put a listener for a # being typed which begins recording the characters following the hash until the user types a space at which time it resets.
if ([text isEqualToString:#"#"]) {
recordingHashTag = YES;
startParse = range.location;
}else if ([text isEqualToString:#" "]) {
currentHashTag = nil;
recordingHashTag = NO;
theTable.hidden = YES;
}
if (recordingHashTag == YES) {
NSString *value;
if (startParse > [textView.text length] - startParse) {
value = [textView.text substringWithRange:NSMakeRange(startParse, [textView.text length] - startParse)];
[self filterHashTagTableWithHash:value];
}
}
If the BOOL recordingHashTag is set to YES I pass the substring containing the hashtag text to a function which searches a pre populated array of hashtags. If there is a match it adds that entry to a filtered array of hashtags which it uses to populate the tableview on the fly.
-(void)filterHashTagTableWithHash:(NSString *)hash{
[self.filterHashTagArray removeAllObjects];
for (NSString *hashTag in self.hashTagArray ){
NSRange result = [hashTag rangeOfString:hash options:NSCaseInsensitiveSearch];
if (result.location != NSNotFound) {
[filterHashTagArray addObject:hashTag];
}
}
if (filterHashTagArray.count) {
theTable.hidden = NO;
}else{
theTable.hidden = YES;
}
[self.theTable reloadData];
}
The final step is to insert the hash tag when the user clicks on the entry in the table.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = (UITableViewCell*)[self tableView:theTable cellForRowAtIndexPath:indexPath];
NSString *newString = [textViewComment.text stringByReplacingCharactersInRange:NSMakeRange(startParse, [textViewComment.text length] - startParse) withString:cell.textLabel.text];
textViewComment.text = newString;
}
Just don't forget to clear out your variables when a user backspaces mid hash tag.

How to check if UITextField's text is valid email?

I have a view controller with 3 UITextFields (username, email, and password).
I need a method that checks first, if all fields have text in them, then check if the email's textfield is a valid email, perhaps by checking if it has an # sign in it. Can anyone help with this?
Following code is use for the checking the validation of the email id using the Regex(Regular expresion).
(BOOL) validateEmail: (NSString *) candidate {
NSString *emailRegex = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex]; // return 0;
return [emailTest evaluateWithObject:candidate];
}
This will check a UITextField for a proper email.
Add this method to the textFields delegate then check if the characters it is about to change should be added or not.
Return YES or NO depending on the text fields current text compared to a valid email address:
#define ALPHA #"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
#define NUMERIC #"1234567890"
#define ALPHA_NUMERIC ALPHA NUMERIC
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *unacceptedInput = nil;
if ([[textField.text componentsSeparatedByString:#"#"] count] > 1) {
unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:#".-"]] invertedSet];
} else {
unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:#".!#$%&'*+-/=?^_`{|}~#"]] invertedSet];
}
return ([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] <= 1);
}
To check if a text field is empty or not just use if (myTextField.text.length > 0) {} anywhere in your view controller.
I have used Mimit's solution but modified the emailRegex to allow for longer names such as museum. So the last curly brackets now says {2, 6} not {2, 4}. And I tested it with the longer name and it works. Thanks Mimit for the easy solution.
-(BOOL) validateEmail: (NSString *) candidate {
NSString *emailRegex = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex]; // return 0;
return [emailTest evaluateWithObject:candidate];
}
try this:-
if(![emailTextField.text isEqualToString:#""] && ![userNameTextField.text isEqualToString:#""] && ![passwordTextField.text isEqualToString:#""])
{
NSString *emailRegEx = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegEx];
//Valid email address
if ([emailTest evaluateWithObject:emailTextField.text] == YES)
{
}
else
{
//not valid email address
}
}
else
{
//any of the text field is empty
}
If you are targeting iOS 4.0 or greater, you might also consider NSRegularExpression and do more nuanced checking of the UITextField contents along the lines of this, for example.

UITextField restriction-iphone

I'm having 4 textfields in my application
1.username
2.Email
3.Age
4.Password
User names are 3-25 characters and contain only the characters [a-z0-9]
Age must be between 1-100 inclusive.
Passwords are between 4-12 characters and use only the characters [a-zA-Z0-9]
how can i restrict the textfield with above requirements
please anyone help me out to do this..
Thank you for your effort and consideration.
You can use the methods in the UITextFieldDelegate protocol to validate your fields' content.
More concretely, either you use:
– textFieldShouldEndEditing:
- textFieldShouldReturn:
or you can use:
- textField:shouldChangeCharactersInRange:replacementString:
In the first case, you only validate when the user ends editing the text field; in the second case, you can do the validation at each keystroke.
In all of those methods, you receive an argument textField which you can access like this:
NSString* text = textField.text;
NSUInterger length = [text length];
if (length.....) {
// -- show alert or whatever
return NO;
}
You can validate numbers as the user type by implementing -[UITextField textField:shouldChangeCharactersInRange:replacementString:] method. Do note that this method is called before the change is made, so you need to construct the text that could be the result of the users actions yourself. For example:
-(BOOL)textField:(UITextField*)textField: shouldChangeCharactersInRange:(NSRange*)range
replacementString:(NSString*)string;
{
NSString* text = [textField.text stringByReplacingCharactersInRange:range
withString:string];
// text is now the potential string you should check against.
}
What you do from there is up to your own. Some examples could be:
// Too short?
if ([text length] < 4) ...
// Invalid character?
NSCharacterSet* invalidChars = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
if ([text rangeOfCharacterInSet:invalidChars].location != NSNotFound) ...
For more complex number validation I would use NSNumberFormatter, that has support for validating ranges and more.
You can use UITextFieldDelegate to get done what you want. Assign different values to textfield.tag for each field in - (void)viewDidLoad method and match those tag values to find the relevant field in the (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string.
#define USERNAME_FIELD_TAG 1
#define PASSWORD_FIELD_TAG 2
#define EMAIL_FIELD_TAG 3
#define AGE_FIELD_TAG 4
#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.tab == USERNAME_FIELD_TAG)
{
if([[NSPredicate predicateWithFormat:#"SELF MATCHES[cd] %#", #"[a-z0-9]{3,35}"] evaluateWithObject:string] == FALSE)
{
textField.text = [textField.text stringByReplacingOccurrencesOfString:string withString:#"" options:NSCaseInsensitiveSearch range:range];
[self selectTextForInput:textField atRange:range];
return NO;
}
}
else if (textField.tab == PASSWORD_FIELD_TAG)
{
if([[NSPredicate predicateWithFormat:#"SELF MATCHES[cd] %#", #"[a-zA-Z0-9]{4,12}"] evaluateWithObject:string] == FALSE)
{
textField.text = [textField.text stringByReplacingOccurrencesOfString:string withString:#"" options:NSCaseInsensitiveSearch range:range];
[self selectTextForInput:textField atRange:range];
return NO;
}
}
else if (textField.tab == EMAIL_FIELD_TAG)
{
if([[NSPredicate predicateWithFormat:#"SELF MATCHES[cd] %#", #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"] evaluateWithObject:string] == FALSE)
{
textField.text = [textField.text stringByReplacingOccurrencesOfString:string withString:#"" options:NSCaseInsensitiveSearch range:range];
[self selectTextForInput:textField atRange:range];
return NO;
}
}
else if (textField.tab == AGE_FIELD_TAG)
{
if([[NSPredicate predicateWithFormat:#"SELF MATCHES[cd] %#", #"[1-100]"] evaluateWithObject:string] == FALSE)
{
textField.text = [textField.text stringByReplacingOccurrencesOfString:string withString:#"" options:NSCaseInsensitiveSearch range:range];
[self selectTextForInput:textField atRange:range];
return NO;
}
}
return YES;
}
// place the cursor at given possition
-(void)selectTextForInput:(UITextField *)input atRange:(NSRange)range {
UITextPosition *start = [input positionFromPosition:[input beginningOfDocument]
offset:range.location];
UITextPosition *end = [input positionFromPosition:start
offset:range.length];
[input setSelectedTextRange:[input textRangeFromPosition:start toPosition:end]];
}