Welcome to WebmasterWorld Guest from 23.20.137.66

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

How can I check a string for certain characters?

check password for complexity

     
7:04 pm on Mar 21, 2006 (gmt 0)

5+ Year Member



I need to check a string which will actually be a password. Need to make sure the string contains one lower case letter, one upper case letter and one number. I know I can split the string into characters, but surely there must be a way to check it against the contents of an array such as:
@array1 = (a..z);
@array2 = (A..Z);
@array3 = (1..9);

Help?

8:05 pm on Mar 21, 2006 (gmt 0)

5+ Year Member



comments removed by perl_diver

[edited by: perl_diver at 8:11 pm (utc) on Mar. 21, 2006]

8:11 pm on Mar 21, 2006 (gmt 0)

5+ Year Member



my $string = 'Ponytail23'; 

if ($string =~ /[A-Z]/ and $string =~ /[a-z]/ and $string =~ /\d/) {
print "It's good";
}
else {
print "It's bad";
}

8:29 pm on Mar 21, 2006 (gmt 0)

5+ Year Member



Lol, that is so easy. Show's my lack of Perl knowledge. (You should see the code I did, about 20 lines!).

Thanks!
Rob

8:34 pm on Mar 21, 2006 (gmt 0)

10+ Year Member



Don't forget to check the length and make sure its long enough. Thats easy though use the "length" function.
8:44 pm on Mar 21, 2006 (gmt 0)

5+ Year Member



Yeah, got that. Just didn't know I could use the brackets in =~. Here is what I have now...

#Check for empty string, 1 and 2nd password entries match
&nameerr if (($new1 eq "")¦¦($new2 eq "")¦¦($new1 ne $new2));
#Check password length
&passworderr if (length($new1) < 6);
#Check for complexity
if ($new1 =~ /[A-Z]/ and $new1 =~ /[a-z]/ and $new1 =~ /\d/) {
&set_password;
} else {
&passworderr;
}

I'd show you what I had before, but you'd probably die laughing...

9:21 pm on Mar 21, 2006 (gmt 0)

10+ Year Member



I'd add a useful (imho) advice. Create a JavaScript to do the same checking before sending form data to server. You'll save some time for your clients and (may be) a few clients too for yourself;))
10:40 pm on Mar 21, 2006 (gmt 0)

5+ Year Member



OK, now that you see the basic concept of the validation you wanted to do, I would take it to the next step. Symbols and spaces should probably not be allowed in passwords, so use \W to check for the presence of non-word
characters, anything not belonging to a-zA-Z0-9_

if ($new1 =~ /\W/) {"Error: invalid character in password";}

 

Featured Threads

Hot Threads This Week

Hot Threads This Month