CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to validate special character passwords?

 



Shrutika
Novice

Oct 3, 2013, 3:25 AM

Post #1 of 11 (1035 views)
How to validate special character passwords? Can't Post

Hi, I am writing a program to check if the password entered is bad,strong or excellent. If a password is excellent, the password needs to include letter in capital(atleast one), atleast one digit and atleast one specail character(@,$ etc.) I am able to validate everything except special characters. Please help.Couldnt find the answer,though its such a simple thing Frown


Tejas
User

Oct 3, 2013, 3:43 AM

Post #2 of 11 (1034 views)
Re: [Shrutika] How to validate special character passwords? [In reply to] Can't Post

Need to have a preceeding '\' before the character.

For example if u are searching for $, then it should be \$.

Thanks
Tejas


Shrutika
Novice

Oct 3, 2013, 4:13 AM

Post #3 of 11 (1032 views)
Re: [Tejas] How to validate special character passwords? [In reply to] Can't Post

Thanks Tejas. But isnt there a shorter way? as in like for digits we can use [0-9] or \d. Any such shorthand for special characters?


Tejas
User

Oct 3, 2013, 4:15 AM

Post #4 of 11 (1030 views)
Re: [Tejas] How to validate special character passwords? [In reply to] Can't Post

Can you please paste the code snippet over here, So that i can analyse and understand the requirement .
Just post whatever you have tried


Tejas
User

Oct 3, 2013, 4:21 AM

Post #5 of 11 (1029 views)
Re: [Shrutika] How to validate special character passwords? [In reply to] Can't Post

Character classes also have ordinary and special characters, but the sets of ordinary and special characters inside a character class are different than those outside a character class. The special characters for a character class are -]\^$ and are matched using an escape:
/[\]c]def/; # matches ']def' or 'cdef'
$x = 'bcr';
/[$x]at/; # matches 'bat, 'cat', or 'rat'
/[\$x]at/; # matches '$at' or 'xat'
/[\\$x]at/; # matches '\at', 'bat, 'cat', or 'rat'


Shrutika
Novice

Oct 3, 2013, 4:25 AM

Post #6 of 11 (1027 views)
Re: [Tejas] How to validate special character passwords? [In reply to] Can't Post

Well here's my basic structure of the code

#! /usr/bin/perl

print("Enter password\n");
$in=<STDIN>;
if($in=~ /(?=.*[A-Z])(?=.*[a-z])(?=.*[\W])/)
{
print("Excellent password\n");
}
elsif($in =~ /(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])/)
{
print("Good password \n");
}
elsif($in =~ /(?=.*[A-Z])(?=.*[a-z])/)
{
print("Bad password\n");
}


Tejas
User

Oct 3, 2013, 4:28 AM

Post #7 of 11 (1026 views)
Re: [Tejas] How to validate special character passwords? [In reply to] Can't Post

Perl has several abbreviations for common character classes. (These definitions are those that Perl uses in ASCII-safe mode with the /a modifier. Otherwise they could match many more non-ASCII Unicode characters as well. See Backslash sequences in perlrecharclass for details.)
\d is a digit and represents
[0-9]
\s is a whitespace character and represents
[\ \t\r\n\f]
\w is a word character (alphanumeric or _) and represents
[0-9a-zA-Z_]
\D is a negated \d; it represents any character but a digit
[^0-9]
\S is a negated \s; it represents any non-whitespace character
[^\s]
\W is a negated \w; it represents any non-word character
[^\w]
The period '.' matches any character but "\n"

Above might help you too.


Shrutika
Novice

Oct 3, 2013, 4:31 AM

Post #8 of 11 (1025 views)
Re: [Tejas] How to validate special character passwords? [In reply to] Can't Post

Thanks a lot Tejas. I 'll try the code with the inputs you have givenSmile


Tejas
User

Oct 3, 2013, 5:20 AM

Post #9 of 11 (1022 views)
Re: [Shrutika] How to validate special character passwords? [In reply to] Can't Post

Thanks.
Please do come with the final piece of code, if successful

Thanks


Shrutika
Novice

Oct 8, 2013, 7:14 AM

Post #10 of 11 (995 views)
Re: [Tejas] How to validate special character passwords? [In reply to] Can't Post

#! /usr/bin/perl

print("Enter password\n");
$in=<STDIN>;
if($in=~ /(?=.*[A-Z])(?=.*[a-z])(?=.*[\d])(?=.*[\$\@\!\#\%\^\&\*\(\)])/)
{
print("Excellent password\n");
}
elsif($in =~ /(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])/)
{
print("Good password \n");
}
elsif($in =~ /(?=.*[A-Z])(?=.*[a-z])/)
{
print("Bad password\n");
}


This code works as I want it to. Thanks for the inputs.


Tejas
User

Oct 8, 2013, 8:05 AM

Post #11 of 11 (989 views)
Re: [Shrutika] How to validate special character passwords? [In reply to] Can't Post

Ok cool
So, U have considered the slash in front of all the special char's u wanted.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives