iPhone SDK中的文本字段输入验证

时间:2013-02-26 06:21:59

标签: iphone ios objective-c xcode validation

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

3 个答案:

答案 0 :(得分: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");

希望可以提供帮助。

答案 1 :(得分: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];    
    }
  }  
}

答案 2 :(得分: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;
}