I have some UITextViews. Where User will enter data and this data will be submitted to database using web service.The database shows error with special characters like ‘&’. So I want to block user when he/she tries to submit data with this special characters or there should a message on submitting the data’ I mean How to validate that textview? If somebody knows about it please help me. Thanx a lot.
Share
First way, mentioned all accepted charecters like below sample code and if found out of range characters put message to display.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789"] invertedSet]; if ([textField.text rangeOfCharacterFromSet:set].location != NSNotFound) { NSLog(@"This string contains illegal characters"); return FALSE; } return TRUE; }Or, go with Regular Expression and do the same thing in another way…
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if ([textField.text isMatchedByRegex:@"[^a-zA-Z0-9]"]) { NSLog(@"This string contains illegal characters"); return FALSE; } return TRUE; }