问题
I want to make a UITextField to accept only characters and display error when numbers or special characters are entered.But when Im doing so ,when I enter alphabets also error is displayed.Could not understand where I going wrong?
NSString *FNameReg=@"[A-Za-z]";
NSPredicate *FNametest=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",FNameReg];
if(![FNametest evaluateWithObject:txtfirstname.text])
{
lblvalidateFName.hidden=NO;
[testScroll setContentOffset:CGPointMake( 0 , 74)];
return;
}
BUt now when I give alphabets also then also error is displayed.Y is it so ?
回答1:
Create one method for validation like this:
-(BOOL) validateAlphabets: (NSString *)alpha
{
NSString *abnRegex = @"[A-Za-z]+"; // check for one or more occurrence of string you can also use * instead + for ignoring null value
NSPredicate *abnTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", abnRegex];
BOOL isValid = [abnTest evaluateWithObject:alpha];
return isValid;
}
Now check for the validation where you want like this:
bool checkAlphabets = [self validateAlphabets:txtfirstname.text];
if(!checkAlphabets)
{
NSLog(@"Not Matches..");
}
else
{
NSLog(@"Matches..");
}
回答2:
You can validate the alphabet string with below code.
Using NSPredicate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return [self validateStringWithAlphabet:string];
}
- (BOOL)validateStringWithAlphabet:(NSString *)string {
NSString *stringRegex = @"[A-Za-z]";
NSPredicate *stringPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stringRegex];
return [stringPredicate evaluateWithObject:string];
}
Using NSCharacterSet
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField == yourTextField)
{
NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
}
return YES;
}
回答3:
try below code my friend , if you want to allow only alphabets.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"] invertedSet];
NSString *trimmedReplacement = [[ text componentsSeparatedByCharactersInSet: nonNumberSet] componentsJoinedByString:@""];
return ([text stringByTrimmingCharactersInSet:nonNumberSet].length > 0);
}
let me know it is working or not!!!!
Happy Coding!!!!!
回答4:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *blockedCharacters = [[NSCharacterSet whitespaceCharacterSet] invertedSet]; NSCharacterSet *blockedCharacters2 = [[NSCharacterSet letterCharacterSet]invertedSet];
if([string rangeOfCharacterFromSet:blockedCharacters].location == NSNotFound || [string rangeOfCharacterFromSet:blockedCharacters2].location)
{
// enter your alert
[alert show];
}
return ([string rangeOfCharacterFromSet:blockedCharacters].location == NSNotFound || [string rangeOfCharacterFromSet:blockedCharacters2].location) ;
}
回答5:
Check this out . First if block for backspace , next if to allow only letters , next if to allow only numbers.
- (BOOL)textField:(UITextField *)field shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)characters
{
NSCharacterSet *blockedCharacters = [[NSCharacterSet alphanumericCharacterSet] invertedSet] ;
NSCharacterSet *letters = [NSCharacterSet letterCharacterSet] ;
NSCharacterSet *numbers = [NSCharacterSet decimalDigitCharacterSet] ;
NSLog(@"%@",[blockedCharacters description]);
//for allowing backspace.
if([characters isEqualToString:@""])
{
return true;
}
if(field==self.firstNameTextField || field==self.lastNameTextField || field==self.addrLocalityTextField || field==self.addrRegionField){
if([characters rangeOfCharacterFromSet:letters].location) {
[self shakeWithIterations:0 direction:1 size:4 view:field];
return false;
}
}
if(field==self.addrPostalCodeTextField || field==self.accountNumTextField || field==self.routingNumTextField ){
if( field==self.addrPostalCodeTextField ){
NSUInteger newLength = [addrPostalCodeTextField.text length] + [characters length] - range.length;
return (newLength > 5) ? NO : YES;
}
if([characters rangeOfCharacterFromSet:numbers].location) {
[self shakeWithIterations:0 direction:1 size:4 view:field];
return false;
}
}
return true;}
来源:https://stackoverflow.com/questions/14358309/allowing-only-alphabets-in-textfield