Page 1 of 1

Regular Expressions

Posted: Sun Jul 20, 2003 9:25 pm
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.

Posted: Mon Jul 21, 2003 4:15 am
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

Posted: Mon Jul 21, 2003 9:54 am
by m3rajk
for perl there's an even easier way to do digits: \d

...

Posted: Mon Jul 21, 2003 10:03 am
by kettle_drum
Oh thank you so much.....i cant tell you how much that has been annoying me over the past day.

...

Posted: Mon Jul 21, 2003 10:25 am
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
}

?>