如何限制在UITextField除点和下划线以外的特殊字符?

您好如何限制特殊字符在UITextField除点和下划线? 我已经尝试下面的代码片段,但没有运气

#define ACCEPTABLE_CHARECTERS @" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_." - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *acceptedInput = [NSCharacterSet characterSetWithCharactersInString:ACCEPTABLE_CHARECTERS]; if (![[string componentsSeparatedByCharactersInSet:acceptedInput] count] > 1){ NSLog(@"not allowed"); return NO; } else{ return YES; } } 

尝试下面给出的代码块,它为我工作得很好。

SWIFT 3.0

 let ACCEPTABLE_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_" func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { let cs = NSCharacterSet(charactersIn: ACCEPTABLE_CHARACTERS).inverted let filtered = string.components(separatedBy: cs).joined(separator: "") return (string == filtered) } 

目标C

 #define ACCEPTABLE_CHARACTERS @" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_." - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ACCEPTABLE_CHARACTERS] invertedSet]; NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; return [string isEqualToString:filtered]; } 

希望它也能为你工作。

尝试这个

 NSCharacterSet *set= [NSCharacterSet symbolCharacterSet]; if ([string rangeOfCharacterFromSet:[set invertedSet]].location == NSNotFound) { // valid } else { // invalid } 

你可以用自己的设定

 NSCharacterSet *set= [NSCharacterSet characterSetWithCharactersInString:@"<all your symbols you want to ignore>"]; 

NSString *Regex = @"[A-Za-z0-9^]*";

NSPredicate *TestResult = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];

[TestResult evaluateWithObject:@"YourTestString"];

最后返回布尔值true / false

在方法中

 + (BOOL) validateSpecialCharactor: (NSString *) text { NSString *Regex = @"[A-Za-z0-9^]*"; NSPredicate *TestResult = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex]; return [TestResult evaluateWithObject:text]; } 

Swift 3.1解决scheme


 let ACCEPTABLE_CHARACTERS = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_." func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { let cs = CharacterSet(charactersIn: ACCEPTABLE_CHARACTERS).inverted let filtered: String = (string.components(separatedBy: cs) as NSArray).componentsJoined(by: "") return (string == filtered) } 

Swift 2.2基于Mrunal的回答 :

 let notAllowedCharacters = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_."; func textField( textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { let set = NSCharacterSet(charactersInString: notAllowedCharacters); let inverted = set.invertedSet; let filtered = string .componentsSeparatedByCharactersInSet(inverted) .joinWithSeparator(""); return filtered == string; } 

这可能会帮助你尝试这个..请让我知道

 -(BOOL)isHaveSpecialChar:(NSString*)str{ NSString *customStr = @"~`!@#$%^&*()+=-/;:\"\'{}[]<>^?, "; NSCharacterSet *alphaSet = [NSCharacterSet characterSetWithCharactersInString:customStr]; BOOL isHaveSpecialChar = [[str stringByTrimmingCharactersInSet:alphaSet] isEqualToString:@""]; return !isHaveSpecialChar; } 
 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { let set = NSCharacterSet(charactersIn: "0123456789") let inverted = set.inverted; let filtered = string.components(separatedBy: inverted).joined(separator: "") return filtered == string; 

尝试这个:

 NSString *str = @"___asdf.SHiv._.l.asg. gg_ASdgASG_.......asfgads.g. .._____fdgdsfghsdfgh"; str= [str stringByReplacingOccurrencesOfString:@"." withString:@""]; str=[str stringByReplacingOccurrencesOfString:@"_" withString:@""]; str=[str stringByReplacingOccurrencesOfString:@" " withString:@""]; str=[str stringByTrimmingCharactersInSet:[NSCharacterSet alphanumericCharacterSet]]; if ([str isEqualToString:@""]) { NSLog(@"valid"); } else { NSLog(@"invalid"); }