Check NSString for special characters

后端 未结 5 2026
北恋
北恋 2020-12-23 09:39

I want to check an NSString for special characters, i.e. anything except a-z, A-Z and 0-9.

I don\'t need to check how many special characters are present, or their p

5条回答
  •  既然无缘
    2020-12-23 10:25

    This code allows only numbers in UITextField input.

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
        if ([string rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]].location != NSNotFound)
            return NO;
        else
            return YES;
    
    }
    

提交回复
热议问题