Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

validate alphanumeric value in UITextField

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.

like image 724
Chaaruu Jadhav Avatar asked Apr 01 '14 07:04

Chaaruu Jadhav


3 Answers

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,

like image 87
Lukasz 'Severiaan' Grela Avatar answered Oct 09 '22 06:10

Lukasz 'Severiaan' Grela


 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;

     }
like image 32
Parvendra Singh Avatar answered Oct 09 '22 06:10

Parvendra Singh


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;
like image 43
NeverHopeless Avatar answered Oct 09 '22 05:10

NeverHopeless