简体   繁体   中英

How to avoid special characters (@, #, &, etc.) in UITextfield validation

Code snippet

   NSString *myRegex = @"[A-Z0-9a-z]*";
    NSPredicate *myTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", myRegex];
    NSString *string = [NSString stringWithString:NewPassword.text];
    BOOL valid = [myTest evaluateWithObject:string];

i want to validate abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 those things only

Suppose customer enter special characters (@, #, &,.,},{,*,:, etc.). no need to validate and accept the characters

Ex :

Password : 1234567 Password : qwerty - Need to show Alert

Desire output : Password : 123qwerty45 - No need to show alert

Password : 1234qwer@#$% -accept the password

No need to check special character

Thanks in advance

Try with following method:

- (BOOL)textField:(UITextField *)field shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)characters
{
    if ([replacementString rangeOfCharacterFromSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]].location != NSNotFound) {
        // There are non-alphanumeric characters in the replacement string
     }
}

Following code might help you.

NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789"] invertedSet];
if ([NewPassword.text rangeOfCharacterFromSet:set].location == NSNotFound)
{
    NSLog(@"NO SPECIAL CHARACTER");
}
else
{
    NSLog(@"HAS SPECIAL CHARACTER");
}

The following method may help you:

NSString *string = iTextField.text;
NSCharacterSet *alphanumSet = [NSCharacterSet alphanumCharSet];
NSCharacterSet *numberSet = [NSCharacterSet decimalDigitCharSet];
BOOL isValid= [[string stringByTrimmingCharactersInSet:alphanumSet] isEqualToString:@""] && ![[string stringByTrimmingCharactersInSet:numberSet] isEqualToString:@""];

To restrict entering data in the textfield you should define it in a **shouldChangeCharactersInRange** method

#define VALID_CHARECTERS @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
   NSCharacterSet *validInput = [NSCharacterSet characterSetWithCharactersInString:VALID_CHARECTERS];

   if (![[string componentsSeparatedByCharactersInSet:avalidInput] count] > 1)
      return NO;
   else
      return YES;
}

where VALID_CHARECTERS can be anything you want to include

Assume #$% are the special characters you want. You want at least one special character, with the possability of mixing it with alphanumerical characters.

Your regular expression would then be something like: ([A-Z0-9a-z]*(#|$|%)+[A-Z0-9a-z]*)+

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



    NSCharacterSet *ss = [[NSCharacterSet alphanumericCharacterSet]invertedSet];


    if ([string isEqualToString:@""]) 
    {

        NSString *trimmedReplacement =
        [[ string  componentsSeparatedByCharactersInSet:ss]
         componentsJoinedByString:@""];

    }
    else 
    {
        return ([string stringByTrimmingCharactersInSet:ss].length > 0);
    }

}

return YES;
}

let me know it is working or not!!

Happy Coding!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM