I'm having 1 UITextfield for password in my iPhone application. I want to validate this textfield with the following validation. * Must be at least 6 characters * Must contain alphanumeric value * does not allowed some special characters are $#@~| how can I restrict the UITextField with above requirements. please anyone help me out to do this..
Asked
Active
Viewed 419 times
-3
-
1plenty of examples present on SO you could search, [look](https://www.google.co.in/?gws_rd=ssl#q=uitextfield+alphanumeric+validation). – vaibhav Feb 01 '17 at 10:02
-
1Possible duplicate of [Password validation in UITextField in iOS](http://stackoverflow.com/questions/15132276/password-validation-in-uitextfield-in-ios) – Prissy Eve Feb 01 '17 at 10:10
-
I want to restrict this special character $#@~| and also space's are not allowed. – Narendra Jagne Feb 01 '17 at 11:33
-
What kind of application does not allow special characters in password? – Klaus Byskov Pedersen Feb 01 '17 at 13:50
3 Answers
0
As simple as below.
NSString *passwordRegex = @"^.*(?=.{6,})(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$!&]).*$";
NSPredicate *passwordPred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordRegex];
if (![passwordPred evaluateWithObject:passwordTF.text]) {
[self.view makeToast:@"Password must be at least 6 character, containing lowercase letter, one uppercase letter, digits, and special character (!@#$&)" duration:2.0 position:@"bottom"];
}

Fahim Parkar
- 30,974
- 45
- 160
- 276
0
Check with below methods.Just now I tried and it works.
- (void)textFieldDidEndEditing:(UITextField *)textField{
if([textField.text length]<5){
[self showAlertController:@"Error" passMessage:@"Must be atleast 6 characters"];
}
NSCharacterSet * charSet = [[NSCharacterSet characterSetWithCharactersInString:@" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_."] invertedSet];
if ([textField.text rangeOfCharacterFromSet:charSet].location != NSNotFound){
[self showAlertController:@"Error" passMessage:@"Must not allow special characters $#@~|"];
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
-(void)showAlertController:(NSString *)title passMessage:(NSString *)message{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
}

user3182143
- 9,459
- 3
- 32
- 39
-1
int numberofCharacters = 0;
BOOL lowerCaseLetter,upperCaseLetter,digit,specialCharacter = 0;
if([textField.text length] >= 6){
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
{
//Error" Please Ensure that you have at least one lower case letter, one upper case letter, one digit and one special character"
}
else{
//error number of char must be more or equal to 6
}

Sagar Thukral
- 70
- 2