regex not working for php form validation : <p>Usernames on my website should only contain A-Z,a-z,0-9. No spaces or special characters.</p>

<p>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?:</p>

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