I want alphanumeric value in textfield.If user enter only character or number then sending massage.Even no special characters acceptable.
        NSString *str = askIdTxt.text;
        NSCharacterSet *alphanumericSet = [NSCharacterSet alphanumericCharacterSet];
        NSCharacterSet *numberSet = [NSCharacterSet decimalDigitCharacterSet];
        BOOL isAlphaNumericOnly = [[str stringByTrimmingCharactersInSet:alphanumericSet] isEqualToString:@""] && ! [[str stringByTrimmingCharactersInSet:numberSet] isEqualToString:@""];
        if (isAlphaNumericOnly) {
             NSLog(@"isAplhaNumericOnly: %@",(isAlphaNumericOnly? @"Yes":@"No"));
        }
This is always returning true. I am not getting what is wrong in this.
How about using regular expression:
-(BOOL)isAlphaNumericOnly:(NSString *)input 
{
    NSString *alphaNum = @"[a-zA-Z0-9]+";
    NSPredicate *regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", alphaNum];
    return [regexTest evaluateWithObject:input];
}
and then use it
if([self isAlphaNumeric:str])
{
    NSLog(@"IT IS ALPHA NUMERIC STRING");
}
edit The same technique can be used to validate passwords, you need only better regex:
-(BOOL)isPasswordStrong:(NSString *)password {
/*
8-20 chars
at least one letter
at least one number OR special character
no more than 3 repeated characters
*/
        NSString *strongPass= @"^(?!.*(.)\\1{3})((?=.*[\\d])(?=.*[A-Za-z])|(?=.*[^\\w\\d\\s])(?=.*[A-Za-z])).{8,20}$";;
        NSPredicate *regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", strongPass];
        return [regexTest evaluateWithObject:password];
}
using the regular expression you can create different rules but this can give you a headstart,
 Call this Method and modify it accordingly .....
   -(BOOL) isPasswordValid:(NSString *)pwd 
     {
       if ( [pwd length]<4 || [pwd length]>20 ) return NO;  // too long or too short
       NSRange rang;
       rang = [pwd rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet]];
       if ( !rang.length ) return NO;  // no letter
       rang = [pwd rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]];
       if ( !rang.length )  return NO;  // no number;
       return YES;
     }
                        I guess the problem is in the alphanumericCharacterSet, here is a part from doc:
Informally, this set is the set of all characters used as basic units of alphabets, syllabaries, ideographs, and digits.
So, I am expecting it would allow the unwanted characters to you.
You may also try using Regex:
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[a-z\\d]+$" options:NSRegularExpressionCaseInsensitive error:&error];
NSUInteger matches = [regex numberOfMatchesInString:str options:NSMatchingReportCompletion range:NSMakeRange(0, [str length])];
BOOL hasMatches = (matches > 0) && !error;
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With