Jump to content

Help with Username and password field validation


mat3000000

Recommended Posts

I am trying to validate username and password fields. I want to use preg match, but have little knowledge of this function. I want the password to only contain A-z 0-9 and with at least one letter and one number.

Username needs to only include "A-z 0-9 _ -" no spaces in any of these. Here is what I have so far:

 

 

$username= $_POST['username'];
$password = $_POST['password'];
$password2 = $_POST['password2'];

if($password==$password2){
if( 
preg_match("[A-z0-9]", $password) ||
strlen($password)>6 // at least 7 chars 
|| strlen($password)<26 // at most 20 chars 
){$errors[] = 'Password must contain at least one number and letter plus be between 7-25 characters. May only contain alphanumeric characters, _ and .';} 
}else{$errors[] = 'Your Passwords did not Match';}


if( 
preg_match("[A-z0-9_-]", $username) ||
strlen($username)>5 // at least 6 chars 
|| strlen($username)<26 // at most 25 chars
){
$errors[] = 'Username must be 6-25 characters and contain only alphanumeric characters, _ and .'; 
}

How would I use this?? I am trying this, but it doesn't seem to be echoing any errors when I just put one letter in the textbox:

 

if($password==$password2){
if( 
preg_match('/^.*(?=.{7,20})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/', $password)
){$errors[] = 'Password Invalid';} 
}else{$errors[] = 'Your Passwords did not Match';}

if( 
preg_match('/^[a-zA-Z0-9\_\-]{5,26}$/', $username)
){
$errors[] = 'Username Invalid'; 
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.