Filtering characters entered into a UITextField

前端 未结 7 1006
猫巷女王i
猫巷女王i 2020-12-14 03:54

I have a UITextField in my application. I\'d like to restrict the set of characters that can be can be entered into the field to a set that I have defined. I could filter th

相关标签:
7条回答
  • 2020-12-14 04:17

    How about this?

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
        NSString* regex = @"[^a-z]";
        return ([[string lowercaseString] rangeOfString: regex
                          options:NSRegularExpressionSearch].location == NSNotFound);
    };
    

    Note:

    I am making all characters to lower case [string lowercaseString] so that you don't need to write in regex for captial/small letters.

    0 讨论(0)
  • 2020-12-14 04:18

    This is what I use to restrict the user to uppercase A-Z. Adjust the regex variable according to taste:

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
        NSString* regex = @"[^A-Z]";
        return ([string rangeOfString: regex
                          options:NSRegularExpressionSearch].location == NSNotFound);
    };
    
    0 讨论(0)
  • 2020-12-14 04:19

    Look at the UITextViewDelegate method - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string.

    It's exactly what you need.

    0 讨论(0)
  • 2020-12-14 04:20

    I did as marcc suggested and it worked well. Sample implementation follows.

    Note: Variable names were selected for brevity and do not reflect my coding standards:

        ...
        myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"xyzXYZ"];
        ...
    }
    
    - (BOOL) textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)textEntered {
        for (int i = 0; i < [textEntered length]; i++) {
            unichar c = [textEntered characterAtIndex:i];
            if (![myCharSet characterIsMember:c]) {
                return NO;
            }
        }
        return YES;
    }
    
    0 讨论(0)
  • 2020-12-14 04:27

    Here is one of the cleanest approaches to restricting characters entered in a UITextField. This approach allows the use of multiple predefined NSCharacterSets.

    -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
        NSMutableCharacterSet *allowedCharacters = [NSMutableCharacterSet alphanumericCharacterSet];
        [allowedCharacters formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
        [allowedCharacters formUnionWithCharacterSet:[NSCharacterSet symbolCharacterSet]];
    
        if([string rangeOfCharacterFromSet:allowedCharacters.invertedSet].location == NSNotFound){
    
            return YES;
    
        }
    
        return NO;
    
    }
    
    0 讨论(0)
  • 2020-12-14 04:34

    You could loop and keep checking if the UITextField.text property has changed once the DidBeginEditing method gets called. If it has, check the text and remove an bad characters.

    0 讨论(0)
提交回复
热议问题