2011-08-05 22 views
0

我想在iPhone應用程序中開發一個功能,使用UITextfield驗證用戶名和密碼,並檢查用戶名是否已經存在,密碼必須至少包含6個字符。如何驗證UITextfield中的用戶名和密碼?

請給我任何鏈接或任何想法來開發此功能。

在此先感謝。

+0

你怎麼要檢查如果用戶名已經存在於您的驗證?使用Web服務或本地(在設備上)數據庫? – 2011-08-05 12:44:28

回答

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

} 
+0

非常感謝Control-V –

0

要檢查密碼長度至少爲6個字符:

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

要檢查如果用戶名存在,我現在需要什麼樣的數據庫您使用的是

+0

我使用Sqlite數據庫 –

0

你可以通過使用此代碼

- (BOOL)isValid { 

UIAlertView *alert; 

NSString *_regex [email protected]"\\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]; 


     } 

    } 

}