- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
int textLength = [[searchBar text] length];
if (textLength > 0) {
NSString *decimalRegex = @"[0-9]+(([0-9])?)";
NSPredicate *decimalTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", decimalRegex];
BOOL isValidDecimal = [decimalTest evaluateWithObject:[searchBar text]];
if (!isValidDecimal) {
NSString *text = [[searchBar text] substringToIndex:textLength - 1];
[searchBar setText:text];
}
}
}
No comments:
Post a Comment