Jump to content

Problem with regex in a reigstry form


PHPiSean

Recommended Posts

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'); ?>

 

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.