PHPiSean Posted August 22, 2011 Share Posted August 22, 2011 So basically I am trying to make a registry form. In this, one of the checks finds out if the input is only text and numbers, (I also want to know how to allow no spaces). In my code, it will always return "Username can only contain letters and number" no matter the input. I figured it was something wrong with the regex. Here's the code <?php require('includes/header.php'); ?> <?php //VARIABLES $submit = mysql_real_escape_string($_POST['submit']); $username = mysql_real_escape_string($_POST['username']); $user = $_POST['username']; $firstname = mysql_real_escape_string($_POST['firstname']); $lastname = mysql_real_escape_string($_POST['lastname']); $email = mysql_real_escape_string($_POST['email']); $password = mysql_real_escape_string($_POST['password']); $passwordconf = mysql_real_escape_string($_POST['passconf']); $loweruser = strtolower($username); $checkuser = mysql_query("select * from users where username_lower='{$loweruser}'"); $userexists = mysql_fetch_assoc($checkuser); // if(!filter_var($email, FILTER_VALIDATE_EMAIL)) { // echo "Invalid Email"; /*CHECKS * Many checks need to be done, the checks will also be done in order. The order will * be listed here * */ if(isset($submit)) { if(empty($username)||empty($firstname)||empty($lastname)||empty($email)||empty($password)||empty($passwordconf)){ echo('Please fill out all fields!<br/>'); }else{ if($userexists) { echo "Username $username already exists"; }else{ if(strlen($username)>15||strlen($username)<5) { echo "Username must be between 5 and 15 characters"; }else{ if (!preg_match('/^[a-zA-Z0-9]$/', $user)) { echo "Username can only contain letters and numbers"; }else{ } } } } } echo "<table> <form name='login' method ='POST' action='register.php'> <tr><h3>Register your Suriak account and get access to all of the Suriak Networks!<h3></tr> <tr><td>Username</td><td><input type='text' name='username' value=".$username."></td></tr> <tr><td>First Name</td><td><input type='text' name='firstname' value=".$firstname."></td></tr> <tr><td>Last Name</td><td><input type='text' name='lastname' value=".$lastname."></td></tr> <tr><td>Email</td><td><input type='text' name='email' value=".$email."></td></tr> <tr><td>Password</td><td><input type='password' name='password' value=".$password."></td></tr> <tr><td>Confirm Password</td><td><input type='password' name='passconf' value=".$passwordconf."></td></tr> <tr><td><input type='submit' name='submit' value='Register'> </form> </table> "; ?> <?php require('includes/footer.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/245457-problem-with-regex-in-a-reigstry-form/ Share on other sites More sharing options...
JAY6390 Posted August 22, 2011 Share Posted August 22, 2011 if (!preg_match('/^[a-zA-Z0-9]$/', $user)) { should be if (!preg_match('/^[a-zA-Z0-9]+$/', $user)) { Quote Link to comment https://forums.phpfreaks.com/topic/245457-problem-with-regex-in-a-reigstry-form/#findComment-1260691 Share on other sites More sharing options...
PHPiSean Posted August 22, 2011 Author Share Posted August 22, 2011 Ahh thank you! Quote Link to comment https://forums.phpfreaks.com/topic/245457-problem-with-regex-in-a-reigstry-form/#findComment-1260695 Share on other sites More sharing options...
requinix Posted August 22, 2011 Share Posted August 22, 2011 There's also ctype_alnum. Quote Link to comment https://forums.phpfreaks.com/topic/245457-problem-with-regex-in-a-reigstry-form/#findComment-1260711 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.