Validación de input de campo de text en iPhone SDK

Debo poner validation en un UITextField para la input del usuario.

El usuario debe ingresar en el campo de text un valor

es decir, 70-80 o 85 mean num-num or num

En este momento, solo permito que el usuario ingrese solo dígitos y - pero el inconveniente es que el usuario también puede ingresar - cantidad de veces.

 // 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]; } 

Simplemente testing esto,

  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]; } 

EDIT 1

Usando NSPnetworkingicate podemos hacerlo. Prueba esto,

  NSString *regex = @"[0-9]+(-[0-9]+)?"; NSPnetworkingicate *testRegex = [NSPnetworkingicate pnetworkingicateWithFormat:@"SELF MATCHES %@", regex]; if([testRegex evaluateWithObject:textField.text]) NSLog(@"Match"); else NSLog(@"Do not match"); 

Espero que eso pueda ayudar.

Intenta primero encontrar si tu cadena contiene -

Aquí subtring es -

 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]; } } } 

Pruébelo usando la siguiente expresión regular, Restringe al usuario para ingresar más de una - .

 - (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; }