2

i have a text field in Contact screen and the user need to enter email address to send me message. Whats the best way to ensure the user has entered a valid email address such as:

a@b.com / net / org / co.il
abc@gmail.com
abc@yahoo.com

etc..

Thanks

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Xtrician
  • 504
  • 3
  • 5
  • 14
  • The only way to check an email address is valid is to send it an email, and confirm that the email has been received. If you want the user to send you a message, you could use the mail compose view controller - this would then come from one of their configured email accounts on the device. – jrturton Jun 19 '12 at 05:47
  • Yes i know but im doing the send message method by HTTPRequest and not by MFMailComposer. – Xtrician Jun 19 '12 at 10:48

3 Answers3

5

Try the following:

- (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];
}


-(IBAction)btnTapped:(id)sender{

    if([self validateEmail:[txtEmail text]] ==1)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"You Enter Correct Email id." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        [alert release];

    }
    else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"You Enter Incoorect Email id." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        [alert release];
    }
}
Abdullah Md. Zubair
  • 3,312
  • 2
  • 30
  • 39
2

Use this textField delegate function as this will be called on every text entered:

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
 {
      NSString *strEnteredText = textField.text;
     if(strEnteredText.length>0) 
     {
       if([self validateEmail:strEnteredText])
       {
          //Valid email 
          //Use UILabel to give message
         // BOOL email = true to know email is valid when submit button tapped
       }
       else
       {
          //Not Valid email
          //Use UILabel to give message
          // BOOl emaiL = false to know email is valid when submit button tapped
        }
     }
 }

Add this method .h file

 - (BOOL) validateEmail: (NSString *) enteredText 
 {
   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:enteredText];
 }
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
0

Swift

extension String {
    func isValidEmail() -> Bool {
        let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,10}"
        let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
        let result = emailTest.evaluateWithObject(self)
        return result
    }
}

"jim@off.com".isValidEmail() //true
"jim.com".isValidEmail() // false
Nick
  • 1,194
  • 1
  • 10
  • 18