IPhone SDK Email Validation

Assuming I created an IBOutlet UITextField *emailValidate;

And an empty method

 -(IBAction)checkEmail:(id)sender { // Add email validation code here. } 

And associated the File Owner file with the TextField , what code will I need to insert into the method to check the email address? checking that only one '@' is included, and only one '.' switched on?

+4
source share
6 answers

Use the function below ...

 +(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; } 
+18
source

In my case, I use the regex found in this blogpost :

 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])+)\\])"; 
+2
source

You can determine if there is exactly one "@" by dividing the string by "@" and checking 2 pieces.

 int numberOfAtPieces = [[emailValidate.text componentsSeparatedByString:@"@"] count]; if ( numberOfAtPicess != 2 ) { // show error alert } else { // let it through } 
0
source

You can get the code from the following link. Hope this can be helpful

0
source

I used the solution shared by Macarse (large regex) for several weeks with success, but I unexpectedly ran into a problem. For example, it does not pass the test using " test1_iPhone@neywen.net ". Therefore, I decided to return to the simpler SP Varma solution (a small and simple regular expression).

0
source

You can call the following text in a UITextField text:

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

Apply emailRegex regex to your needs.

-1
source

Source: https://habr.com/ru/post/1340193/


All Articles