bt707
asked on
Perl - check new password for user name
I have a snip of code in a script where users reset their password, here doing a check to see make sure the user does not use their name in their new password.
I'm trying to use the Q and E, which I thought would strip out the special characters and numbers but guess not.
From the script below, how can I get a match if the new password contains the users name? I guess I could do a substitute and remove all numbers and any special characters found, any suggestions?
Thanks,
I'm trying to use the Q and E, which I thought would strip out the special characters and numbers but guess not.
From the script below, how can I get a match if the new password contains the users name? I guess I could do a substitute and remove all numbers and any special characters found, any suggestions?
Thanks,
#! /usr/bin/perl
use strict;
use warnings;
my $pass = 'Jimmy#123';
my $username = 'Jimmy5';
# My own name? switched username and pass around (sundaram)
if ( (defined $username)
&& ($username ne "")
&& (($username =~ /\Q$pass\E/i) ||
($pass =~ /\Q$username\E/i)
)
) {
print "MATCH\n";
} else {
print "NO Match\n";
}
You have it backwards. It should be $pass =~ /$username/ since you want to check the password to see if it contains the username.
ASKER
wilcoxon,
I'm checking it both ways, to see if the password contains the user name and also if the new password contains the name.
So already have what you showed in the code, have it both with an OR
&& (($username =~ /\Q$pass\E/i) ||
($pass =~ /\Q$username\E/i)
what am I missing?
I'm checking it both ways, to see if the password contains the user name and also if the new password contains the name.
So already have what you showed in the code, have it both with an OR
&& (($username =~ /\Q$pass\E/i) ||
($pass =~ /\Q$username\E/i)
what am I missing?
Hmm. I need to read closer - you have both conditions. \Q and \E say that what is between them is a literal quote (disabling pattern metacharacters (not to strip out anything).
$pass =~ /\Q$username\E/ should handle matching the username as part of the password.
Your code is not matching because the password does not contain the username. If you want to strip out non-word characters first then you should add the below two lines (or only one of them depending on which you want stripped) before the if conditions.
$username =~ s{[^a-zA-Z]}{}g;
$pass =~ s{[^a-zA-Z]}{}g;
$pass =~ /\Q$username\E/ should handle matching the username as part of the password.
Your code is not matching because the password does not contain the username. If you want to strip out non-word characters first then you should add the below two lines (or only one of them depending on which you want stripped) before the if conditions.
$username =~ s{[^a-zA-Z]}{}g;
$pass =~ s{[^a-zA-Z]}{}g;
#! /usr/bin/perl
use strict;
use warnings;
my $pass = 'Jimmy#123';
my $username = 'Jimmy5';
# My own name? switched username and pass around (sundaram)
if ( (defined $username)
&& ($username ne "")
&& (($username =~ /\Q$pass\E/i) ||
($pass =~ /\Q$username\E/i)
)
) {
print "MATCH\n";
} else {
print "NO Match\n";
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok, this will help get what I want, I was thinking the Q and E was suppose to do something else then what it's doing and you pointed out.
I was getting what I want from just stripping the numbers from the username - $username =~ s/[0-9]//g; - but that will not work for all users, what you added and gave me should get me where I wanted to be.
Thanks!!!
I was getting what I want from just stripping the numbers from the username - $username =~ s/[0-9]//g; - but that will not work for all users, what you added and gave me should get me where I wanted to be.
Thanks!!!