38

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

10 に答える 10

109

以下のコードブロックを試してみてください、それは私にとってはうまくいきました。

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

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

それがあなたにもうまくいくことを願っています。

于 2013-01-28T09:13:21.157 に答える
4

これを試して

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>"];
于 2013-01-28T08:27:30.043 に答える
3

ムルナルの答えに基づくSwift 2.2 :

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;

}
于 2016-04-28T21:58:34.493 に答える
3

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];
}
于 2013-10-16T10:07:32.777 に答える
1

これはあなたがこれを試すのを助けるかもしれません..そして私に知らせてください

-(BOOL)isHaveSpecialChar:(NSString*)str{
NSString *customStr = @"~`!@#$%^&*()+=-/;:\"\'{}[]<>^?, ";
NSCharacterSet *alphaSet = [NSCharacterSet characterSetWithCharactersInString:customStr];
BOOL isHaveSpecialChar = [[str stringByTrimmingCharactersInSet:alphaSet] isEqualToString:@""];
return !isHaveSpecialChar;
}
于 2013-01-28T08:19:47.200 に答える
1
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;
于 2016-11-02T12:50:09.373 に答える
-1

これを試して:

NSString *str = @"___asdf.SHiv._.l.asg.  g.g._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");
    }
于 2013-01-28T09:10:56.453 に答える