不允许在文本字段中使用A-Z和0-9编号

时间:2018-05-22 11:19:42

标签: ios objective-c validation

我想在我的项目中实现一些验证,验证是:不允许UITextField中的数字0-9和A-Z,只允许UITextField中的GUJARATI字母。我已经尝试了一些验证,但它无法正常工作。我需要任何人的帮助。

我试过以下代码:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    //limit the size :
    if([self checkValidation:textField] == true)
    {
        return NO;
    }
    return YES;
}
- (BOOL)checkValidation:(UITextField *)textField
{
    NSString *rejex = [NSString stringWithFormat:@"%@",@"[A-Za-z0-9]"];
    NSPredicate *gujTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", rejex];

    //if rejex fullfil than it will return true else false.
    return [gujTest evaluateWithObject:textField.text];
}

TIA

4 个答案:

答案 0 :(得分:0)

对于此问题,最好使用CharacterSet检测。正则表达式与unicode不能很好地协作。此外,最好检查您想要允许的字符,而不是相反:

let text = textField.text ?? ""
// https://unicode.org/charts/PDF/U0A80.pdf 0x0A80 ... 0x0AFF
let gurajatiCharacterSet = CharacterSet(charactersIn: Unicode.Scalar(0x0A80 as Int)! ...  Unicode.Scalar(0x0AFF as Int)!)
return text.rangeOfCharacter(from: gurajatiCharacterSet.inverted) == nil

目标-C:

NSUInteger minValue = 0x0A80;
NSUInteger maxValue = 0x0AFF;
NSCharacterSet *gurajatiCharacterSet = [NSCharacterSet characterSetWithRange:NSMakeRange(minValue, maxValue - minValue + 1)];

return [textField.text rangeOfCharacterFromSet:[gurajatiCharacterSet invertedSet]].location == NSNotFound;

答案 1 :(得分:0)

//Example var one=[{'a':'first'},{'b':'second'}] var two=one.slice() var three=two.slice(0,1) three[0].a='third' //Now in all variables (one,two,three) a will have value of 'three' 函数中,您将checkValidation作为参数并提取其textField属性 - 但是您在text中调用它,并且到那时{ {1}}属性在更新之前仍然保留文本的旧值(在从此函数返回shouldChangeCharactersInRange后它会更新)。改为

text

如果您只是不想允许使用拉丁字母数字字符,请更改true

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    //limit the size :
    if([self checkValidation:string] == true)
    {
        return NO;
    }
    return YES;
}

- (BOOL)checkValidation:(NSString *)string
{
    let stringWithGujaratiCharacters = "અ" // string with all Gujarati letters
    let characterSet = CharacterSet(charactersIn: stringWithGujaratiCharacters).inverted // character set containing every character besides Gujarati letters
    return string.rangeOfCharacter(from: characterSet) != nil // returns true if string contains characters other than Gujarati letters
}

答案 2 :(得分:0)

在下面的代码中将textfield更改为字符串。它会起作用。

if([self checkValidation:string] == true)

- (BOOL)checkValidation:(NSString *)textField
{
    NSString *rejex = [NSString stringWithFormat:@"%@",@"[A-Za-z0-9]"];
    NSPredicate *gujTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", rejex];

     //if rejex fullfil than it will return true else false.
    return [gujTest evaluateWithObject:textField];
}

答案 3 :(得分:-1)

对于此问题,您可以使用NSCharacterSet。

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{

    NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"];
    for (int i = 0; i < [string length]; i++)
    {
        unichar c = [string characterAtIndex:i];
        if (![myCharSet characterIsMember:c])
        {
            return NO;
        }
    }
    return YES;
}