Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
653 views
Welcome To Ask or Share your Answers For Others

1 Answer

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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...