2011-06-24 19 views

回答

1

要驗證字符,只有你可以使用此代碼

NSString *trimmed3 = [textfield.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
BOOL isNumeric = [trimmed3 length] >0 && [trimmed3 rangeOfCharacterFromSet:nonNumberSet].location== NSNotFound; 

if (!isNumeric) { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"oops" 
                message:@"Phone no should be numeric" 
                delegate: self 
              cancelButtonTitle: @"Continue" 
              otherButtonTitles: nil]; 
    [alert show]; 
    [alert release]; 
} 
+0

請格式化發佈前的內容。它將幫助每個人 - 現在或未來。謝謝。 –

1

對於條件1:

#define REG_EX_USERNAME_VALIDATION @"[a-zA-Z\\d]" 
NSPredicate *userNameValidation = [ NSPredicate predicateWithFormat:@"SELF MATCHES %@", REG_EX_USERNAME_VALIDATION]; 
int retVal = [ userNameValidation evaluateWithObject:inPasskey]; 

對於條件2:您可以使用此委託方法

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
// checking for any whitespaces 
if(([string rangeOfString:@" "].location != NSNotFound)) 
{ 
    return NO; 
} 
return YES; 
}