Reputation: 57
Password validation, every thing is ok, but it allows blank spaces also, how to restrict that one, after entering the password it won't delete it. my password length is 8 characters, it allow only 8 or 4, but its not accept 5 to 7 characters. This is my code. Please check it once. Thanks in advance.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if([passwrd.text length] < 8)
return YES;
else
return NO;
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
//int numberofCharacters = 0;
BOOL lowerCaseLetter = '\0',upperCaseLetter = '\0',digit = '\0',specialCharacter = 0;
if([passwrd.text length] >= 8)
{
for (int i = 0; i < [passwrd.text length]; i++)
{
unichar c = [passwrd.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 && [[NSCharacterSet punctuationCharacterSet] characterIsMember:c]) {
specialCharacter = YES;
}
}
if(specialCharacter && digit && lowerCaseLetter && upperCaseLetter)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success"
message:@"Successfully Created Password"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
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 Only 8 Characters"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[passwrd resignFirstResponder];
return YES;
}
Upvotes: 0
Views: 3361
Reputation: 5448
Replace your shouldChangeCharactersInRange
method with this
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(string.length == 0){
// Returning yes here to allow entry of backspace
return YES;
}
if([string isEqualToString:@" "]){
// Returning no here to restrict whitespace
return NO;
}
if([passwrd.text length] < 8)
return YES;
else
// By returning no here, you are restricting the user to enter no more
// than 8 characters. You should not do this ideally. So, after the user
// enters 8 characters, even backspace will not be registered.
// For the user to be able to enter backspace, I added the first condition
// above in the method.
return NO;
}
Edit:
To accept passwords below 8 characters, you will need to entirely change your textFieldDidEndEditing
method. What is the \0
character? That is totally wrong. As @gnasher729 suggests, initialise your booleans with a YES
or NO
, not \0
.
The
if([passwrd.text length] >= 8)
condition in textFieldDidEndEditing
is not what you wait IMO. You are doing the password checks if text length is greater than or equal to 8. What you should do is check for text length less than or equal to 8, because anyways, more than 8 letters are not accepted.
So, replace it with this
if([passwrd.text length] <= 8)
Upvotes: 2
Reputation: 52622
Read the NSString documentation and look for a method that checks if a string contains any character from a character set. That will very much reduce the code in your test for all the characters.
Use reasonable variable names. lowerCaseLetter is a fine name for a variable containing a lowercase letter. It is an awful name for a boolean variable which describes whether a string contains a lowercase letter or not. Call it "hasLowercaseLetter" or "containsLowercaseLetter". If you can't find good variable names, you will get yourself in trouble.
What is this '\0' about? That is godawful. '\0' is a char. Initialise BOOLs to YES or NO. And one variable per line. This is just awful. And why is your code so inconsistent that you have to use two different ways to check the next character? If I see that someone uses two different ways to achieve the same goal, I expect at least one bug, sometimes two.
Why on earth are you trying to limit password length? That's ridiculous. And obviously I can enter more than eight characters using a little trick, but it should also be obvious that when the user has entered eight characters, you don't allow any changes anymore! Why do you think is there a parameter "range" and a parameter "replacementString"? Can you think how their values could affect what you should return in shouldChangeCharactersInRange?
Upvotes: 1
Reputation: 1102
You can use NSRegularExpression to solve this problem. Its quite easier to implement. Here is a quick link for you. Regular Expression for password in iPhone
Also here is a great tutorial if you really want to go into details and understand how it works. http://www.raywenderlich.com/30288/nsregularexpression-tutorial-and-cheat-sheet
Hope this helps you. Cheers! :)
Upvotes: 0