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 .'; 
}

Link to comment
Share on other sites

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'; 
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.