PHP form validation using preg_match for password checks

This is a general question on password form validation and using a combination of uppercase, lowercase, numbers and characters used.

Research has showed that preg_match is required to validate the password (or whatever variable you use): (preg_match("/^.(?=.{8,})(?=.[0-9])(?=.[a-z])(?=.[A-Z]).*$/")

Though how would how would i integrate this into the if statement below? I've tried combining them using && though this seems to ignore the preg_match part. if(($pass == $pass2) && (preg_match("/^.(?=.{8,})(?=.[0-9])(?=.[a-z])(?=.[A-Z]).*$/"))

Any advice would be appreciated.

<?php

require "dbconn.php";

$username = ($_GET['username']);
$email = ($_GET['email']);
$pass = ($_GET['pwd1']);
$pass2 = ($_GET['pwd2']);
$USN = ($_GET['schoolnumber']);

$matching = 0;

if($pass == $pass2)
{
echo "<script type='text/javascript'> window.alert('Your details have been successfully    registered, please proceed to login with your new credentials!')</script>";
echo '<script>javascript:window.close();</script>';


$query = "INSERT INTO customer VALUES     ('".$username."','".$email."','".$pass."','face1.jpg','".$USN."','N')";

$results = mysql_query($query) or die (mysql_error());

$results1 = mysql_query($query1) or die (mysql_error());
}
else{

header('Location:register.php?matching=1');

}
?>

Answers


preg_match requires two arguments the pattern to look for and the string to test. Plus I'm not sure if the regular expression you are using would work anyway. Try something like.

$pattern = '/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}/';
if (($pass1 == $pass2) && preg_match($pattern, $pass1)) {
    ...
}

You only need to match against one of the passwords because should ($pass1 == $pass2) fail then the preg_match isn't even preformed.

The regex above checks that the password is at least 8 characters long and contains at least one of each of lowercase, uppercase and a number


Need Your Help

Javascript code not working in Firefox

javascript html firefox

I have a dropdown list box. If a user hasn't selected anything, it should not go to next page. For that I tried something. That code works fine in IE and Chrome. But it's going next page in Firefox...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.