6

在我的应用程序中。我想用特殊字符验证文本字段,

前任。如果用户按?从键盘比用户无法输入?在文本字段中,

请任何人建议,我该怎么做?

4

3 回答 3

5

像下面这样尝试它会帮助你。它只接受字母

   - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        //For avoiding user to enter non digital number..
        if([[string stringByTrimmingCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]] isEqualToString:@""])
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
于 2012-04-18T07:57:27.843 回答
0

检查这个

利用

- (BOOL) textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)textEntered
于 2012-04-18T07:47:50.493 回答
0

看看这是否有效:

    -(NSString *) formatIdentificationNumber:(NSString *)string
     {
       NSCharacterSet * invalidNumberSet = [NSCharacterSet characterSetWithCharactersInString:@"\n_!@#$%^&*()[]{}'\".,<>:;|\\/?+=\t~` "];

    NSString  * result  = @"";
    NSScanner * scanner = [NSScanner scannerWithString:string];
    NSString  * scannerResult;

    [scanner setCharactersToBeSkipped:nil];

     while (![scanner isAtEnd])
     {
    if([scanner scanUpToCharactersFromSet:invalidNumberSet intoString:&scannerResult])
    {
        result = [result stringByAppendingString:scannerResult];
    }
    else
    {
        if(![scanner isAtEnd])
        {
            [scanner setScanLocation:[scanner scanLocation]+1];
        }
    }
 }

  return result;
  }
于 2012-04-18T07:50:44.467 回答