仅允许textField中的字母表

时间:2013-01-16 12:18:29

标签: iphone uitextfield xcode4.5

我想让UITextField只接受字符并在输入数字或特殊字符时显示错误。但是当我这样做时,当我输入字母时也会显示错误。不能理解我哪里出错?

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

现在,当我给出字母表时,也会显示错误然后显示错误。是吗?

5 个答案:

答案 0 :(得分:8)

创建一个验证方法,如下所示:

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

现在检查您想要的验证:

bool checkAlphabets = [self validateAlphabets:txtfirstname.text];

if(!checkAlphabets)
{
   NSLog(@"Not Matches..");
}
else
{
  NSLog(@"Matches..");
}

答案 1 :(得分:6)

您可以使用以下代码验证字母表字符串。

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

使用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;
 }

答案 2 :(得分:2)

尝试下面的代码我的朋友,如果你想只允许使用字母。

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

}

让我知道它是否有效!!!!

快乐编码!!!!!

答案 3 :(得分:2)

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

}

答案 4 :(得分:1)

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