3

我必须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];
    }
4

3 回答 3

4

试试这个,

    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");

希望能有所帮助。

于 2013-02-26T06:33:48.510 回答
1

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

这里的减法是-

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];    
    }
  }  
}
于 2013-02-26T06:38:10.380 回答
0

尝试使用以下正则表达式,它限制用户输入多个-

- (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;
}
于 2013-02-26T06:35:28.200 回答