开发者

How to validate username and password in a UITextfield?

I want to develop a functi开发者_C百科onality 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.


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];
}

}


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


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];


      }   

    }

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜