【问题标题】:Textfield Input Validation in iPhone SDKiPhone SDK 中的文本字段输入验证
【发布时间】:2013-02-26 06:21:58
【问题描述】:

我必须对UITextField 进行验证以供用户输入。

用户必须在文本字段中输入一个值

即70-80 或 85 表示 num-num 或 num

目前,我只允许用户输入数字和-,但缺点是用户还可以输入- 的次数。

// My code is as follow

NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789-"] invertedSet];

    if (([txtMarks.text rangeOfCharacterFromSet:set].location != NSNotFound )||[txtMarks.text isEqualToString:@""] ) {

        UIAlertView *alt=[[UIAlertView alloc]initWithTitle:@"Error" message:@"Invalid Input" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alt show];
        [alt release];
    }

【问题讨论】:

  • 您希望允许用户添加像这样的 70-80 或 85 的值。用户可以像这样输入 34-35-38 吗?
  • 不,只有 num-num 或 num。

标签: iphone ios objective-c xcode validation


【解决方案1】:

试试这个,

    int times = [[txtMarks.text componentsSeparatedByString:@"-"] count]-1;
    if(times>1)
    {
        UIAlertView *alt=[[UIAlertView alloc]initWithTitle:@"Error" message:@"'-' used more than one" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alt show];
        [alt release];    
    }

编辑 1

使用NSPredicate 我们可以做到。试试这个,

    NSString *regex = @"[0-9]+(-[0-9]+)?";
    NSPredicate *testRegex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

    if([testRegex evaluateWithObject:textField.text])
        NSLog(@"Match");
    else
        NSLog(@"Do not match");

希望能有所帮助。

【讨论】:

  • 它允许用户首先输入-
【解决方案2】:

先试试这个,看看你的字符串是否包含-

这里的子字符串是-

if ([txtMarks.text hasPrefix:@"-"]||[txtMarks.text hasSuffix:@"-"])
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"sorry " message:@"invalid inoput as it has - at start or end" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
    [alert release];
}
else
{
  NSRange textRange;
  textRange =[string rangeOfString:substring];

  if(textRange.location == NSNotFound)
  {
      //Does not  contain the substring
     NSlog(@" string contains only num")

  }
  else
  {
     int times = [[txtMarks.text componentsSeparatedByString:@"-"] count];
     if(times==2)
     {
         Nslog(@"num-num input")

     }
     else
    {
       UIAlertView *alt=[[UIAlertView alloc]initWithTitle:@"Error" message:@"'-' used more than one" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
         [alt show];
         [alt release];    
    }
  }  
}

【讨论】:

  • 感谢您的回复.. 但它永远不会进入 num-num 输入
  • 但是这个用户也可以在第一个字符处输入-
  • 现在检查它,如果用户输入“-”作为第一个或最后一个字符,它会发出警报
  • 伟大的编码..!!非常感谢......但很抱歉说我已经接受了答案,所以我只是对你的答案投了赞成票。
  • ur Wc,没有问题,您总是选择最佳答案,以便将来可以帮助某人。
【解决方案3】:

试试下面的正则表达式,它限制用户输入多个-

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    NSString *expression = @"^([0-9]{1,}+)?(\\-([0-9]{1,})?)?$";

    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
                                                        options:0
                                                          range:NSMakeRange(0, [newString length])];
    if (numberOfMatches == 0)
    {
        return NO;
    }
    return YES;
}

【讨论】:

  • 为什么要否决这段代码中的错误。我根据问题编写了相同的代码和工作。
  • 我没有对你投反对票!其他任何人都做到了。我正在尝试你的代码。 &也感谢您的回复..
  • 将上述代码写在shouldChangeCharactersInRange方法中,允许用户在输入第二个-.
  • 您缺少return 声明。另外,if 语句不应该是if (numberOfMatches != 1)吗?
  • @Girish 有一些问题,比如它首先允许输入-,当我尝试写 80-90 时,它只允许输入 80-9
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
相关资源
最近更新 更多