Regular Expressions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Regular Expressions

Post by kettle_drum »

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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

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>';
}
?>
Mac
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

for perl there's an even easier way to do digits: \d
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

...

Post by kettle_drum »

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

...

Post by kettle_drum »

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
}

?>
Post Reply