I have this regular expression
`NSString *expression = @"^([0-9]+)?(\\.?\\,?([0-9]{1,2})?)?$";`
but i need a limit to 6 number before the dot or the comma and permit always two decimals.
Each caracters is checked with regular expression if it is valid i can write.
The problem is that i can write XXXXXXXX (8 numbers) but not six numbers or (XXXXXX.XX) (8 numbers and 2 decimals) for example with this expression
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == _priceTf)
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSString *expression = @"^([0-9]{1,6})?(\\.?\\,?([0-9]{1,2})?)?$";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression
options:NSRegularExpressionCaseInsensitive
error:nil];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
options:0
range:NSMakeRange(0, [newString length])];
if (numberOfMatches == 0)
return NO;
}
return YES;
}
Aucun commentaire:
Enregistrer un commentaire