2011-08-12 7 views
0

私は自分のアプリケーションで4つのテキストフィールドUITextFieldの制限 - iphone

1.username 2.Email 3.Age 4.Password

ユーザー名は3-25文字であるとだけ含まれているを持っています文字[a-z0-9]

年齢は1〜100の間である必要があります。

パスワードは4-12文字の間にあると私は誰もがこれを行うために私を助けてください、上記の要件

でテキストフィールドを制限することができますどのように文字のみ[-ZA-Z0-9]

を使用します..

ありがとうございました。

答えて

1

UITextFieldDelegateプロトコルのメソッドを使用して、フィールドの内容を検証することができます。

より具体的には、どちらかの使用:

あなたが使用することができます
– textFieldShouldEndEditing: 
- textFieldShouldReturn: 

か:ユーザーがテキストフィールドを編集して終了したときに

- textField:shouldChangeCharactersInRange:replacementString: 

最初のケースで、あなただけの検証。 2番目のケースでは、各キーストロークで検証を行うことができます。これらの方法の全てにおいて

、あなたは、このようにアクセスできる引数textFieldを受け取る:

NSString* text = textField.text; 
NSUInterger length = [text length]; 
if (length.....) { 
// -- show alert or whatever 
return NO; 
} 
+0

:しかし、私はusername-whatのためにA-Zと0-9だけを必要としますか? – Prajan

+0

それには 'NSRegularExpression'を使用してください:http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html – sergio

1

あなたは-[UITextField textField:shouldChangeCharactersInRange:replacementString:]メソッドを実装することで、ユーザのタイプとして数字を検証することができます。変更が行われる前にの前にこのメソッドがと呼ばれているので、ユーザーの操作の結果である可能性のあるテキストを構築する必要があります。例:

-(BOOL)textField:(UITextField*)textField: shouldChangeCharactersInRange:(NSRange*)range 
                 replacementString:(NSString*)string; 
{ 
    NSString* text = [textField.text stringByReplacingCharactersInRange:range 
                  withString:string]; 
    // text is now the potential string you should check against. 
} 

自分で行うことはあなた自身の責任です。いくつかの例は次のようになります。より複雑な数の検証のために、私は範囲などを検証するためのサポートを持っていること、NSNumberFormatterを使用することになり

// Too short? 
if ([text length] < 4) ... 

// Invalid character? 
NSCharacterSet* invalidChars = [[NSCharacterSet alphanumericCharacterSet] invertedSet]; 
if ([text rangeOfCharacterInSet:invalidChars].location != NSNotFound) ... 

+0

上記のコードは私にerror.location undeclaredを示しています – Prajan

0

UITextFieldDelegateを使用して、必要な処理を行うことができます。 - (void)viewDidLoadメソッドの各フィールドのtextfield.tagに異なる値を割り当て、それらのタグ値を一致させて(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)stringの関連フィールドを見つけます。

#define USERNAME_FIELD_TAG 1 
#define PASSWORD_FIELD_TAG 2 
#define EMAIL_FIELD_TAG 3 
#define AGE_FIELD_TAG 4 

#pragma mark - UITextFieldDelegate 
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
    if (textField.tab == USERNAME_FIELD_TAG) 
    { 
     if([[NSPredicate predicateWithFormat:@"SELF MATCHES[cd] %@", @"[a-z0-9]{3,35}"] evaluateWithObject:string] == FALSE) 
     { 
      textField.text = [textField.text stringByReplacingOccurrencesOfString:string withString:@"" options:NSCaseInsensitiveSearch range:range]; 
      [self selectTextForInput:textField atRange:range]; 
      return NO; 
     } 
    } 
    else if (textField.tab == PASSWORD_FIELD_TAG) 
    { 
     if([[NSPredicate predicateWithFormat:@"SELF MATCHES[cd] %@", @"[a-zA-Z0-9]{4,12}"] evaluateWithObject:string] == FALSE) 
     { 
      textField.text = [textField.text stringByReplacingOccurrencesOfString:string withString:@"" options:NSCaseInsensitiveSearch range:range]; 
      [self selectTextForInput:textField atRange:range]; 
      return NO; 
     } 
    } 
    else if (textField.tab == EMAIL_FIELD_TAG) 
    { 
     if([[NSPredicate predicateWithFormat:@"SELF MATCHES[cd] %@", @"[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"] evaluateWithObject:string] == FALSE) 
     { 
      textField.text = [textField.text stringByReplacingOccurrencesOfString:string withString:@"" options:NSCaseInsensitiveSearch range:range]; 
      [self selectTextForInput:textField atRange:range]; 
      return NO; 
     } 
    } 
    else if (textField.tab == AGE_FIELD_TAG) 
    { 
     if([[NSPredicate predicateWithFormat:@"SELF MATCHES[cd] %@", @"[1-100]"] evaluateWithObject:string] == FALSE) 
     { 
      textField.text = [textField.text stringByReplacingOccurrencesOfString:string withString:@"" options:NSCaseInsensitiveSearch range:range]; 
      [self selectTextForInput:textField atRange:range]; 
      return NO; 
     } 
    } 

    return YES; 
} 

// place the cursor at given possition 
-(void)selectTextForInput:(UITextField *)input atRange:(NSRange)range { 
    UITextPosition *start = [input positionFromPosition:[input beginningOfDocument] 
               offset:range.location]; 
    UITextPosition *end = [input positionFromPosition:start 
               offset:range.length]; 
    [input setSelectedTextRange:[input textRangeFromPosition:start toPosition:end]]; 
} 
関連する問題