3

I have to put validation on a UITextField for user input.

The user must input into the textfield a value

i.e. 70-80 or 85 mean num-num or num

Right now, I just allow to user to input only digits& - but drawback is that user can also input - number of times.

// 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 に答える