开发者

How to remove special characters'&' from UItextField iPhone

开发者 https://www.devze.com 2023-03-31 03:23 出处:网络
I searched a lot over Google, but cant find a valid solution for best way to remove special characters\'&\' from Text Field in iPhone

I searched a lot over Google, but cant find a valid solution for best way to remove special characters'&' from Text Field in iPhone

I only want to remove or delimit user to enter '&' keyword.

I know that I have to do something in function

- (BOOL)textField:(UITextField *)textFieldBeingChanged shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

But what exactly not getting it properly.

Thanks开发者_如何学运维 in advance.....


See if this works :

-(NSString *) formatIdentificationNumber:(NSString *)string
{
    NSCharacterSet * invalidNumberSet = [NSCharacterSet characterSetWithCharactersInString:@"\n_!@#$%^&*()[]{}'\".,<>:;|\\/?+=\t~` "];

    NSString  * result  = @"";
    NSScanner * scanner = [NSScanner scannerWithString:string];
    NSString  * scannerResult;

    [scanner setCharactersToBeSkipped:nil];

    while (![scanner isAtEnd])
    {
        if([scanner scanUpToCharactersFromSet:invalidNumberSet intoString:&scannerResult])
        {
            result = [result stringByAppendingString:scannerResult];
        }
        else
        {
            if(![scanner isAtEnd])
            {
                [scanner setScanLocation:[scanner scanLocation]+1];
            }
        }
    }

    return result;
}  
0

精彩评论

暂无评论...
验证码 换一张
取 消