How do I limit a Text Field length AND acceptable characters?

Discussion in 'iOS Programming' started by Jules2010, Apr 19, 2010.

  1. Jules2010 macrumors member

    Joined:
    Apr 7, 2010
    #1
    I've arrived at the code to do either limiting the length OR acceptable characters, but how do I do both for the same field.

    Heres my code
    Code:
    - (BOOL)textField:(UITextField *)textField 
        shouldChangeCharactersInRange:(NSRange)range 
        replacementString:(NSString *)string
    {
        NSString *newString = [textField.text 
            stringByReplacingCharactersInRange:range withString:string];
        return !([newString length] > 3);
    }
    And

    Code:
    (NSRange)range replacementString:(NSString *)string
    {
        NSCharacterSet *nonNumberSet = [[NSCharacterSet 
            characterSetWithCharactersInString:@"0123456789."] invertedSet];
        return ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0);
    }
    Thanks in advance
     

Share This Page