-2

Usernames on my website should only contain A-Z,a-z,0-9. No spaces or special characters.

I have the following php code in my register form however when I input a username which contains only letters it outputs the error, any help?:

if (!preg_match('/[^a-zA-Z0-9]+/gm', $_POST["userName"])) {
    $output = json_encode(
       array(
         'type' => 'error',
         'text' => 'Your username must not contain any spaces or special characters.'
          ));
    die($output);
}

1 Answers1

0

your syntax is wrong,Try these

<?php   
 if (!preg_match('/^[a-zA-Z0-9]+$/', "name")) {
         echo "incorrect";
    }else{
      echo "correct";
    }
?>
Balaji Rajendran
  • 357
  • 5
  • 17