homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

How can I check a string for certain characters?
check password for complexity

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

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);




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

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)

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)

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



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

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)

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/) {
} else {

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


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

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)

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";}

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved