How to check if UITextField's text is valid email?

后端 未结 5 1113
清酒与你
清酒与你 2020-12-28 23:45

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

I need a method that checks first, if all fields have text in them, then check if the e

相关标签:
5条回答
  • 2020-12-28 23:56

    I have used Mimit's solution but modified the emailRegex to allow for longer names such as museum. So the last curly brackets now says {2, 6} not {2, 4}. And I tested it with the longer name and it works. Thanks 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];
    }
    
    0 讨论(0)
  • 2020-12-29 00:13

    try this:-

        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
            }
    
    0 讨论(0)
  • 2020-12-29 00:15

    This will check a UITextField for a proper email.
    Add this method to the textFields delegate then check if the characters it is about to change should be added or not.
    Return YES or NO depending on the text fields 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.

    0 讨论(0)
  • 2020-12-29 00:15

    Following code is use for the checking the validation of the email id using the Regex(Regular expresion).

    (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];
    }
    
    0 讨论(0)
  • 2020-12-29 00:20

    If you are targeting iOS 4.0 or greater, you might also consider NSRegularExpression and do more nuanced checking of the UITextField contents along the lines of this, for example.

    0 讨论(0)
提交回复
热议问题