-1

I want to develop a functionality in an iPhone app to validate username and password using a UITextfield and also to check if the username is already exits and the password must be at least 6 characters.

Please give me any link or any idea to develop this functionality.

Thanks in advance.

1
  • How do you want to check if the username already exists? Using a web service or a local (on the device) database?
    – user142019
    Aug 5, 2011 at 12:44

3 Answers 3

2

We assume that you have already got all the userNames in userNamesArr array

if (pwdTextField && [pwdTextField length] > 6 ){

  for(NSString* existUserName in userNamesArr){
     if(existUserName isEqualToString:txtUserName.text){ //txtUserName is your UItextField
         UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"User name already exists" delegate:self cancelButtonTitle:@"Try with different user name" otherButtonTitles:nil];

        [alert show];
        [alert release];
        return;
    }else{
         // ** Save New User to your database **
    }
}else{
     UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"Password should be atleast 6 characters" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alert show];
    [alert release];
}

}
0
0

To check if the password is at least 6 characters long:

if (textField.text.length > 6){//Do something}

To check if the username exist I need to now what kind of database are you using

0
0

You can go through with this code

- (BOOL)isValid {

UIAlertView *alert;

NSString *_regex =@"\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";

NSPredicate *_predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", _regex];

    //Alert View.
if (self.usernameText.text == nil || [self.usernameText.text length] == 0 || 
    [[self.usernameText.text stringByTrimmingCharactersInSet:[NSCharacterSet  whitespaceAndNewlineCharacterSet]] length] == 0 ) {

    alert = [[UIAlertView alloc]initWithTitle:@"Attention" message:@"Please enter email address." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alert show];

    return FALSE;

} 
else if (![_predicate evaluateWithObject:self.usernameText.text] == YES) {

    alert = [[UIAlertView alloc]initWithTitle:@"Attention" message:@"Please enter your correct email address." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alert show];

    return FALSE;

} else if (self.passwordText.text == nil || [self.passwordText.text length] == 0 
   ||[[self.passwordText.text stringByTrimmingCharactersInSet:[NSCharacterSet 
whitespaceAndNewlineCharacterSet]] length] == 0 ) {

    alert = [[UIAlertView alloc]initWithTitle:@"Attention" message:@"Please enter your 

password." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alert show];

    return FALSE;


} 


return TRUE;


}

and then check your validation on the log in button action

-(IBAction)loginAction:(id)sender{

    if(loginBtn.tag == 10){

    if ([self isValid]) {

            [self performSelector:@selector(checkLogin) withObject:nil afterDelay:0.0];


      }   

    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.