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
https://forums.phpfreaks.com/topic/245457-problem-with-regex-in-a-reigstry-form/
Share on other sites

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.