Reputation: 3701
I have lots of textfields in one view and instead of user filling all the details and hit save button, I want to validate the email id entered there only. How to give the error message in the label when the user goes to the next textField
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:self.emailidTexField.text] == NO)
{
// UILabel besides the text field and when the user goes to next field logic here.
}
Upvotes: 1
Views: 1128
Reputation: 170
This may help... try this
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if ([textField isEqual:emailidTexField])
{
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:self.emailidTexField.text] == NO)
{
[emailidTexField becomeFirstResponder];
}
else{
[Yournexttextfield becomeFirstResponder];
}
}
}
Upvotes: 2
Reputation: 20551
use this bellow code..
Here when you click on return or Next key of keyBoard at that time this bellow textFieldShouldReturn:
UITextField
delegate method called and you can write any validation or code with its condition bellow ...
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField==self.emailidTexField) {
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:self.emailidTexField.text] == NO)
{
// UILabel besides the text field and when the user goes to next field logic here.
[self.emailidTexField resignFirstResponder];
[YourNextTextFieldName becomeFirstResponder];
}
else{
// Display message here that EmaiId is not valid
return NO;
}
}
return YES;
}
here i put the condition that if textField is self.emailidTexField then go inside and check validation if its true then it resign that textField keyboard and go cursor on NextTextField.
Upvotes: 2
Reputation: 5886
You have to set delegate of textfield and write following code in UITextField
delegate method
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if ([textField isEqual:emailidTexField])
{
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:self.emailidTexField.text] == NO)
{
[emailidTexField becomeFirstResponder];
}
else{
[Yournexttextfield becomeFirstResponder];
}
}
}
Upvotes: 0
Reputation: 7226
Simple when the user goes to the next UITextField
. Check in the
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
whether it is the same UITextField
or not , either you can set tags for the UITextField's
to find out. Then just add a UIAlertView
there like :-
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
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:self.emailidTexField.text] == NO)
{
UIAlertView *_alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Email is Invalid !!! " delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
}
}
Upvotes: 1
Reputation: 96
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:self.emailidTexField.text] == NO)
{
[your next field becomesfirstresponder];
}
Make your next textfield firstresponder to move the control to next field.
Upvotes: 1
Reputation: 119031
You can add yourself as the delegate of the text field and perform your validation in textFieldDidEndEditing:
. Be sure to call endEditing:
on the view when the save button is pressed to ensure that the delegate method runs.
Upvotes: 1
Reputation: 1739
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"%d",textFiled.tag);
if(textField.tag==xxx){
//check valid here
}
return YES;
}
Upvotes: 1