仅允许UITextView中的字母数字字符

时间:2013-04-22 12:24:01

标签: objective-c xcode uitextview nscharacterset

无论如何,我可以允许用户只在文本视图中输入字母数字字符而不能输入其他字符。

修改

试过了,

if ([_txtView.text rangeOfCharacterFromSet:alphaSet].location != NSNotFound)
    {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"Only alpha numeric characters are allowed" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        return;

    }

但这只适用于某些时候

谢谢!

5 个答案:

答案 0 :(得分:13)

您可以使用[[[NSCharacterSet alphanumericCharacterSet] invertedSet]来实现。此方法将返回仅包含接收器中不存在的字符的字符集。

NSCharacterSet *charactersToBlock = [[NSCharacterSet alphanumericCharacterSet] invertedSet];

//符合UITextField委托并实现此方法。

 - (BOOL)textField:(UITextField *)field shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)characters
 {
    return ([characters rangeOfCharacterFromSet:charactersToBlock].location == NSNotFound);
 }

答案 1 :(得分:1)

试试这个:

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
            if (textField == txtWebsite) {
                    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "];

                    if ([string rangeOfCharacterFromSet:set].location != NSNotFound) {
                        return YES;
                    }
                    else {
                        return NO;
                    }
            }
            else {
                    return YES;
            }
    }

在uitextfield的委托方法中编写代码。

答案 2 :(得分:1)

为textview设置委托,覆盖/实现测试应该在范围方法

中更改
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
{
     NSCharacterSet *alphaSet = [NSCharacterSet alphanumericCharacterSet];
     BOOL valid = [[text stringByTrimmingCharactersInSet:alphaSet] isEqualToString:@""];
     return valid;    
}

答案 3 :(得分:0)


//在ViewDidLoad或任何init方法中添加它 NSCharacterSet * blockedCharacters = [[[NSCharacterSet alphanumericCharacterSet] invertedSet] retain];

然后在nib文件中设置文本字段的委托。

- (BOOL)textField:(UITextField *)field shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)characters
{
    return ([characters rangeOfCharacterFromSet:blockedCharacters].location == NSNotFound);
}

shouldChangeCharactersInRange方法还有另一种方法。你可以检查

{
    NSString *stringPlace = @"[a-z  A-Z]*";

    NSPredicate *testPlace = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stringPlace]; 
        BOOL matches = [testPlace evaluateWithObject:string];

        if (!matches && string.length > 5)
        {
            return NO;
        }
    return YES;
}

答案 4 :(得分:0)

@ user1113101

提供的等效Swift 3版本的答案

尽管回答起来很晚,还有其他简单而有效的方法,但这个答案可能对某些人有用。

这很简单,对我来说就像是一种魅力。

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    /// 1. replacementText is NOT empty means we are entering text or pasting text: perform the logic
    /// 2. replacementText is empty means we are deleting text: return true
    if text.characters.count > 0 {
        var allowedCharacters = CharacterSet.alphanumerics

        let unwantedStr = text.trimmingCharacters(in: allowedCharacters)
        return unwantedStr.characters.count == 0
    }

    return true
}

注意:这也适用于将字符串粘贴到文本字段中。如果粘贴的字符串包含任何不需要的字符,则不会在文本字段中显示。