Xcode UITextField限制字符类型

时间:2014-03-18 05:58:04

标签: objective-c uitextfield

在Xcode中,如何用“ABCD1234567”限制UITextField的字符,前4个字符是没有特殊字符的字母表,其余字符必须有6或7个数字。 请帮我解决这个问题。谢谢你的进步。

2 个答案:

答案 0 :(得分:1)

定义以下变量

#define NUMBERS @"0123456789"
#define ALPHABATES @"ABCDEFGHIJKLMNOPQRSTUVWXYZ"

现在在textfield的委托函数中,

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs;
    NSString *filtered;
    //for maximum characters
    if (textField.text.length >= 11 && range.length == 0)
        return NO;

    if(textField.text.length<11)
    {
        NSString *cStr=[NSString stringWithFormat:@"%@%@",textField.text,string];
        if([cStr length]<11)
        {
            if(range.location<4)
            {

                cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHABATES] invertedSet];
                filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
                return [string isEqualToString:filtered];
            }
            else
            {

                cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];
                filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
                return [string isEqualToString:filtered];
            }
        }
        else
        {
            int iPrevLength,iNextLength;
            iPrevLength=[textField.text length];
            iNextLength=11;
            int iLengthDiff=iNextLength-iPrevLength;
            string=[string substringToIndex:iLengthDiff];
            if(range.location<4)
            {

                cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHABATES] invertedSet];
                filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
                return [string isEqualToString:filtered];
            }
            else
            {

                cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];
                filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
                return [string isEqualToString:filtered];
            }
            if([string isEqualToString:filtered])
            textField.text=[NSString stringWithFormat:@"%@%@",textField.text,string];
            return NO;
        }
    }

    return YES;
}

确保ALPHABATES有大写字母,因此它只适用于像A,B C这样的大写字母而不是a,b,c ....

首先接受ALPHABATES的4个字母,然后接受6个7个字母的数字。

我希望它对你有用。

让我知道。

答案 1 :(得分:-1)

//this method is delegate method of UITextField
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    int strLen = [textField.text length];
    BOOL invalidChar=FALSE;


if (strLen<=11)
{
    for (int i=0,j=1;i<strLen;i++,j++)
    {
        char strChar = [textField.text characterAtIndex:i];

        if (j<=4)
        {
            if (strChar<65 || strChar>90)
            {
                invalidChar=TRUE;
                break;
            }
        }

        if (j>=5)
        {
            if (strChar<48 || strChar>57)
            {
                invalidChar=TRUE;
                break;
            }
        }
    }
}
else
{
    [txtString becomeFirstResponder];
}

if (invalidChar)
{
    [txtString becomeFirstResponder];
}
}