How to check the correctness of the text UITextField?

I have a view controller with 3 UITextFields (username, email address and password).

I need a method that checks first if all the fields have text in them, and then check if the email text field is a valid email address, perhaps by checking if there is an @ sign in it. Can anyone help with this?

+7
source share
5 answers

This will check the UITextField for the correct email.
Add this method to the textFields delegate, then check to see if the characters it should change are added or not.
Return YES or NO depending on the text fields of the current text compared to a valid email address:

 #define ALPHA @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" #define NUMERIC @"1234567890" #define ALPHA_NUMERIC ALPHA NUMERIC - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *unacceptedInput = nil; if ([[textField.text componentsSeparatedByString:@"@"] count] > 1) { unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:@".-"]] invertedSet]; } else { unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:@".!#$%&'*+-/=?^_`{|} ~@ "]] invertedSet]; } return ([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] <= 1); } 

To check if a text field is empty or not just use if (myTextField.text.length > 0) {} anywhere in your view controller.

+4
source

The following code is used to verify the validation of an email identifier using a regular expression (Regex).

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

I used the Mimit solution, but modified emailRegex to use longer names such as a museum. Therefore, the last braces now say that {2, 6} is not {2, 4}. And I tested it with a longer name, and it works. Thanks to Mimit for the easy solution.

 -(BOOL) validateEmail: (NSString *) candidate { NSString *emailRegex = @"[A-Z0-9a-z._%+-] +@ [A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; // return 0; return [emailTest evaluateWithObject:candidate]; } 
+5
source

try the following: -

  if(![emailTextField.text isEqualToString:@""] && ![userNameTextField.text isEqualToString:@""] && ![passwordTextField.text isEqualToString:@""]) { NSString *emailRegEx = @"[A-Z0-9a-z._%+-] +@ [A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx]; //Valid email address if ([emailTest evaluateWithObject:emailTextField.text] == YES) { } else { //not valid email address } } else { //any of the text field is empty } 
+3
source

If you configure iOS 4.0 or higher, you can also consider NSRegularExpression and do a finer check on the contents of the UITextField along the lines of this , for example.

0
source

All Articles