Always Learn
Always Learn

Reputation: 305

Validating Text fields texts in ios

I'm facing problems while validating the TextFields.

My problem is, I have 3 text fields.

1.Name (I want 8 to 20 characters including small and upper case letters)

2.Email Id (Valid email id)

3.Password (It should be Strong i.e, 8 to 20 characters including small case,uppercase,numbers and at least one special character)

I had solved the first two conditions with regx.I had stucked in password validating condition,When I used same regx in Password in that time I entered correct one also it shows Wrong password alert. I'm using like below.

NSString *passwordRegex =@" ^.*(?=.{8,})(?=.*d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$";
NSPredicate *passwordTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordRegex];
BOOL bool = [passwordTest evaluateWithObject:password.text];

if (bool==NO) {
    UIAlertView *messageBox = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Title", nil) message:NSLocalizedString(@"Invalid Password", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil];[messageBox show];
    [messageBox release]; 
    passwordValid=0;
}else passwordValid=1;

Thanks in Advance

Upvotes: 1

Views: 1397

Answers (2)

SAMIR RATHOD
SAMIR RATHOD

Reputation: 3506

Try this :

    ^.*(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[\d\W]).*$

and check this

http://www.zorched.net/2009/05/08/password-strength-validation-with-regular-expressions/

Upvotes: 0

Deepesh Gairola
Deepesh Gairola

Reputation: 1242

Try this code

BOOL passwordValid;
NSString *passwordRegex =@"^.*(?=.{8,})(?=.*d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$";
NSPredicate *passwordTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordRegex];
NSString *stringWithPass = @"C0mp@redText";
BOOL isPasswordValid = [passwordTest evaluateWithObject:stringWithPass];

if (isPasswordValid==NO) {
    UIAlertView *messageBox = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Title", nil) message:NSLocalizedString(@"Invalid Password", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil];
    [messageBox show];
    passwordValid=0;
}else passwordValid=1;

I think you have to remove space from where the string starts ^

Upvotes: 1

Related Questions