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



Shrutika
Novice

Oct 3, 2013, 3:25 AM


Views: 2958
How to validate special character passwords?

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


Views: 2957
Re: [Shrutika] How to validate special character passwords?

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


Views: 2955
Re: [Tejas] How to validate special character passwords?

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


Views: 2953
Re: [Tejas] How to validate special character passwords?

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


Views: 2952
Re: [Shrutika] How to validate special character passwords?

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


Views: 2950
Re: [Tejas] How to validate special character passwords?

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


Views: 2949
Re: [Tejas] How to validate special character passwords?

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


Views: 2948
Re: [Tejas] How to validate special character passwords?

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


Tejas
User

Oct 3, 2013, 5:20 AM


Views: 2945
Re: [Shrutika] How to validate special character passwords?

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

Thanks


Shrutika
Novice

Oct 8, 2013, 7:14 AM


Views: 2918
Re: [Tejas] How to validate special character passwords?

#! /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


Views: 2912
Re: [Shrutika] How to validate special character passwords?

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