Assuming I have created IBOutlet UITextField *emailValidate;
And the empty method
-(IBAction)checkEmail:(id)sender {
// Add email validation code here.
}
And linked the File Owner file to the TextField
, what code would I have to insert in the method to validate an email adress? checking that only one '@' is included, and only one '.' is included?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With