开发者

How to perform password validations in iPhone? [closed]

开发者 https://www.devze.com 2023-03-14 04:05 出处:网络
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this po
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

开发者_JS百科

Closed 4 years ago.

Improve this question

I have an application in which I have to perform validations on password and confirm password. This is my criteria for password field:

  1. First letter should be capital.
  2. Password should be more than above 6 values.
  3. At least one special key should be present.
  4. At least 1 numeric value should be entered in password.

If all this criteria are present then only password is accepted.


This should work:

- (BOOL)passwordIsValid:(NSString *)password {

    // 1. Upper case.
    if (![[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[password characterAtIndex:0]])
        return NO;

    // 2. Length.
    if ([password length] < 6)
        return NO;

    // 3. Special characters.
    // Change the specialCharacters string to whatever matches your requirements.
    NSString *specialCharacters = @"!#€%&/()[]=?$§*'";
    if ([[password componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:specialCharacters]] count] < 2)
        return NO;

    // 4. Numbers.
    if ([[password componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"0123456789"]] count] < 2)
        return NO;

    return YES;
}

Testing it out:

NSLog(@"%@", [self passwordIsValid:@"Test#one"] ? @"YES" : @"NO"); // Prints NO.
NSLog(@"%@", [self passwordIsValid:@"Test#1"] ? @"YES" : @"NO"); // Prints YES.


This is the code: what you need to do is, just manipulate regular expressin according to your need,

-(IBAction)passwordValidator:(id)sender{
    NSString *pwd=[NSString stringWithString:passwordField.text];
    int lngth=[pwd length]; 
    int minlength=6;

    NSString *regex = @"\\b([a-zA-Z0-9]+)\\b"; 

    NSPredicate * regextest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

    BOOL x= [regextest evaluateWithObject:pwd];

    if (lngth>=minlength) {
        NSLog(@"passoword length is enough");
        if (x==FALSE) {
            //DO something
        }
    }
    else {
        //DO other thing
    }
}

You Can Use Following set of validations.


Use DHValidation for your all validations. It is much easier to use.

0

精彩评论

暂无评论...
验证码 换一张
取 消