In my App I take one textfield for email and another one for username and button, write code under button event after enter data click button display another view,my problem is write code for email validation(aaa@gmail.com in this format) these alert view is display when button click (msg-enter correct email format) but I want display these alert move to next textfiled suppose I entered email in wrong format and movie to next field it username textfiled at that time email alert view will display.
Asked
Active
Viewed 1,704 times
3 Answers
2
Use NSPredicate
and Regex
:
- (BOOL)validateEmailString:(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];
}
For emails separated by a comma (,):
- (NSMutableArray*)validateEmailWithString:(NSString*)emails
{
NSMutableArray *emails = [[NSMutableArray alloc] init];
NSArray *emailOfArray = [emails componentsSeparatedByString:@","];
for (NSString *email in emailOfArray)
{
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:email])
[emails addObject:email];
}
return [emails autorelease];
}

Jhaliya - Praveen Sharma
- 31,697
- 9
- 72
- 76
0
I have validated my fields on editingDidEnd event and used following code:
- (IBAction) emailValidation:(id)sender {
NSString *eml=((UITextField *)sender).text;
NSString *regex = @"\\b([a-zA-Z0-9%_.+\\-]+)@([a-zA-Z0-9.\\-]+?\\.[a-zA-Z]{2,6})\\b";
NSPredicate * regextest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL x= [regextest evaluateWithObject:eml];
if (x==FALSE) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Errror!" message:@"You have entered incorrect email ID" delegate:self cancelButtonTitle:@"Wanna Correct" otherButtonTitles:nil];
[alert show];
[emailField becomeFirstResponder];
[alert release];
}
}
-(IBAction)passwordValidator:(id)sender{
NSString *pwd=[NSString stringWithString:passwordField.text];
int lngth=[pwd length];
int minlength=6;
NSString *regex = @"\\b([a-zA-Z0-9]+)\\b";
NSPredicate * regextest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL x= [regextest evaluateWithObject:pwd];
if (lngth>=minlength) {
NSLog(@"passoword length is enough");
if (x==FALSE) {
NSLog(@"Special charector check enabled");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"No Special Charectors" message:@"please don't use special charectors" delegate:self cancelButtonTitle:@"Wanna Correct" otherButtonTitles:nil];
[alert show];
[alert release];
[passwordField becomeFirstResponder];
[self.view addSubview:passwordField];
}
}
else {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Poor length" message:@"Password length must not be less than 8.." delegate:self cancelButtonTitle:@"Wanna Correct" otherButtonTitles:nil];
[alert show];
[alert release];
[passwordField becomeFirstResponder];
}
}
Try these, call then on any relevant event, You'll have desired result. good Luck :)

rptwsthi
- 10,094
- 10
- 68
- 109