Hi,
Ive just spent the last 2 or so hours trying to write a regular expression to deal with checking users passwords.
All i want is for the expression to make sure that the user has inputted at least 1 Uppecase letter, 1 Lowercase letter and 1 number in the password.
Can anybody help me?
Thanks in advance.
Regular Expressions
Moderator: General Moderators
-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Code: Select all
<?php
$password = 'Testing123';
if (!preg_match('/[A-Z]+/', $password)) {
echo '<p>you need at least one uppercase letter in the password</p>';
}
if (!preg_match('/[a-z]+/', $password)) {
echo '<p>you need at least one lowercase letter in the password</p>';
}
if (!preg_match('/[0-9]+/', $password)) {
echo '<p>you need at least one number (0-9) in the password</p>';
}
?>-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
...
Oh thank you so much.....i cant tell you how much that has been annoying me over the past day.
-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
...
This is the code i used if anybody else has problems with it in the future:
Code: Select all
<?php
$password = $_POST[password];
if(!preg_match("/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s)/", $password)){
#Password doesnt contain 1 Upper, 1 Lower and 1 number
}else{
#password is good
}
?>