Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Password validation in UITextField in iOS

I have 1 UITextfield for password in my iPhone application.

I want to validate this textfield with the following validation.

  • Must be at least 10 characters
  • Must contain at least one lower case letter, one upper case letter, one digit and one special character
  • Valid special characters are – @#$%^&+=^.*(?=.{10,})(?=.*d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$

How can I restrict the UITextField with above requirements?

like image 365
ChandreshKanetiya Avatar asked Feb 28 '13 09:02

ChandreshKanetiya


2 Answers

This is how I would do it. The validation should be done at the end when the user has typed in the password and not in between.I will not be using NSRegularExpression.

-(void)textFieldDidEndEditing:(UITextField *)textField{
   int numberofCharacters = 0;
   BOOL lowerCaseLetter,upperCaseLetter,digit,specialCharacter = 0;
   if([textField.text length] >= 10)
   {
      for (int i = 0; i < [textfield.text length]; i++) 
      {
         unichar c = [textfield.text characterAtIndex:i];
         if(!lowerCaseLetter)
         {
           lowerCaseLetter = [[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:c];
         }
         if(!upperCaseLetter)
         {
           upperCaseLetter = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:c];
         }
         if(!digit)
         {
            digit = [[NSCharacterSet decimalDigitCharacterSet] characterIsMember:c];
         }
         if(!specialCharacter)
         {
            specialCharacter = [[NSCharacterSet symbolCharacterSet] characterIsMember:c];
         }
      }

      if(specialCharacter && digit && lowerCaseLetter && upperCaseLetter)
      {
         //do what u want
      }
      else
      { 
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"Please Ensure that you have at least one lower case letter, one upper case letter, one digit and one special character"
                                                       delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
      }

   }
   else
   {
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"Please Enter at least 10 password"
                                                       delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
   }
}

Hope this helps...

like image 192
lakshmen Avatar answered Oct 21 '22 21:10

lakshmen


You can also do this by using Regex. Here are few example I am providing for you:

// *** Validation for Password ***

    // "^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$" --> (Minimum 8 characters at least 1 Alphabet and 1 Number)
    // "^(?=.*[A-Za-z])(?=.*\\d)(?=.*[$@$!%*#?&])[A-Za-z\\d$@$!%*#?&]{8,16}$" --> (Minimum 8 and Maximum 16 characters at least 1 Alphabet, 1 Number and 1 Special Character)
    // "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d]{8,}$" --> (Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet and 1 Number)
    // "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!%*?&])[A-Za-z\\d$@$!%*?&]{8,}" --> (Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character)
    // "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!%*?&])[A-Za-z\\d$@$!%*?&]{8,10}" --> (Minimum 8 and Maximum 10 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character)

Fourth from the list is your case, following code snippet shows how to use it:

-(BOOL)isValidPassword:(NSString *)passwordString
{
    NSString *stricterFilterString = @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!%*?&])[A-Za-z\\d$@$!%*?&]{10,}";
    NSPredicate *passwordTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stricterFilterString];
    return [passwordTest evaluateWithObject:passwordString];
}

Using the method:

if(![self isValidPassword:txtPassword.text]) {
    /* Show alert: "Password must be minimum 10 characters,
       at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 
       1 Number and 1 Special Character" */
}
else {
    // Password is valid
}
like image 18
NSPratik Avatar answered Oct 21 '22 19:10

NSPratik