如何验证UITextfield中的用户名和密码?

时间:2011-08-05 12:40:37

标签: iphone uitextfield

我想在iPhone应用程序中开发一个功能,使用UITextfield验证用户名和密码,并检查用户名是否已经退出,密码必须至少为6个字符。

请给我任何链接或任何想法来开发此功能。

提前致谢。

3 个答案:

答案 0 :(得分:2)

  

我们假设您已经拥有userNamesArr数组中的所有userNames

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

}

答案 1 :(得分:0)

检查密码是否至少6个字符:

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

要检查用户名是否存在,我现在需要使用哪种数据库

答案 2 :(得分:0)

您可以使用此代码

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


}

然后在登录按钮操作

上检查您的验证
-(IBAction)loginAction:(id)sender{

    if(loginBtn.tag == 10){

    if ([self isValid]) {

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


      }   

    }

}