mat3000000 Posted February 4, 2011 Share Posted February 4, 2011 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 .'; } Quote Link to comment Share on other sites More sharing options...
artur.kaze Posted February 4, 2011 Share Posted February 4, 2011 $userRegEx = '/^[a-zA-Z0-9\_\-]{5,26}$/'; $passRegEx = '/^.*(?=.{7,20})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/'; I did not tested here, but i'm almost sure that it gonna work. Quote Link to comment Share on other sites More sharing options...
mat3000000 Posted February 4, 2011 Author Share Posted February 4, 2011 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'; } Quote Link to comment Share on other sites More sharing options...
mat3000000 Posted February 4, 2011 Author Share Posted February 4, 2011 Sorry, forget the '!' before preg_match. Works fine now. Thanks for your help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.