<p>So I am writing a registration form and I need the display name to be only numbers, letters and underscores. </p>

<p>Have a look at my code and tell me what I'm doing wrong.</p>

<pre><code>&lt;form method="post" action="/" onsubmit="return check_form()"&gt;
    &lt;input type="text" id="display-name" name="display-name" maxlength="255" /&gt;
    &lt;input type="submit" /&gt;
&lt;/form&gt;
&lt;script type="text/javascript"&gt;
&lt;!--
    var name_regex = /^([a-zA-Z0-9_])+/

    function check_form()
    {
        if (!name_regex.test(document.forms[0].elements[0].value))
        {
            document.forms[0].elements[0].focus()
            alert("Your display name may only contain letters, numbers and underscores")
            return false
        }
    }
--&gt;
&lt;/script&gt;
</code></pre>

<p>It's obviously been trimmed down to not include anything not related to the problem but even this snippet doesn't work.</p>

[{'answer_id': 18874, 'author': 'AnnanFay', 'author_id': 2118, 'author_profile': 'https://Stackoverflow.com/users/2118', 'pm_score': 3, 'selected': False, 'text': "<p>My regexp would go along the lines of: <code>/^[a-zA-Z0-9_]+$/</code></p>\n\n<p>edit: I think it's the lack of a line end <code>$</code> that makes it fail.</p>\n"}, {'answer_id': 18881, 'author': 'samjudson', 'author_id': 1908, 'author_profile': 'https://Stackoverflow.com/users/1908', 'pm_score': 0, 'selected': False, 'text': "<p>By 'not working' I take it you mean it is letting invalid entries through (rather than not letting valid entries through).</p>\n\n<p>As @Annan has said, this would probably be due to the lack of the <code>$</code> character at the end of the expression, as currently it only requires a single valid character at the start of the value, and the rest can be anything.</p>\n"}, {'answer_id': 18884, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 5, 'selected': True, 'text': "<p>Your regex</p>\n\n<pre><code>/^([a-zA-Z0-9_])+/\n</code></pre>\n\n<p>Looks for </p>\n\n<ol>\n<li>Start of string(check), followed by</li>\n<li>1 or more letters, numbers, or underscore (check)</li>\n</ol>\n\n<p>And then whatever comes after it doesn't matter. This regex will match anything at all so long as it begins with a letter, number, or underscore</p>\n\n<p>If you put a <code>$</code> at the end, then it will work - <code>$</code> matches 'end of string', so the only way it can match is if there are <em>only</em> numbers, letters, and underscores between the start and end of the string.</p>\n\n<pre><code>/^([a-zA-Z0-9_])+$/\n</code></pre>\n\n<p>Secondly, I'd suggest using <code>document.getElementById('display-name').value</code> instead of <code>document.forms</code> as it won't break if you rearrange the HTML, and is more 'the commonly accepted standard of what to do'</p>\n"}, {'answer_id': 18892, 'author': 'Chris Conway', 'author_id': 1412, 'author_profile': 'https://Stackoverflow.com/users/1412', 'pm_score': 0, 'selected': False, 'text': '<p>What does "doesn\'t work" mean? Does it reject valid display names? Does it accept invalid display names? Which ones?</p>\n\n<p>Per @Annan, leaving off the <code>$</code> would make the regexp accept invalid display names like <code>abc123!@#</code>.</p>\n\n<p>If the code is rejecting valid display names, it may be because the parentheses are being matched literally instead of denoting a group (I\'m not sure of the quoting convention in JS).</p>\n'}, {'answer_id': 18903, 'author': 'Andy', 'author_id': 1993, 'author_profile': 'https://Stackoverflow.com/users/1993', 'pm_score': 0, 'selected': False, 'text': '<p>I tested your script and meddled with the javascript. This seem to work: </p>\n\n<pre><code>&lt;form method="post" action="/" onsubmit="return check_form()"&gt;\n    &lt;input type="text" id="display-name" name="display-name" maxlength="255" /&gt;\n    &lt;input type="submit" /&gt;\n&lt;/form&gt;\n&lt;script type="text/javascript"&gt;\n    &lt;!--\n    var name_regex = /^([a-zA-Z0-9_])+$/;\n\n    function check_form()\n    {\n        if (!name_regex.test(document.forms[0].elements[0].value))\n        {\n            document.forms[0].elements[0].focus();\n            alert("Your display name may only contain letters, numbers and underscores");\n            return false;\n        }\n    }\n    --&gt;\n&lt;/script&gt;\n</code></pre>\n'}, {'answer_id': 18997, 'author': 'Andrew G. Johnson', 'author_id': 428190, 'author_profile': 'https://Stackoverflow.com/users/428190', 'pm_score': 0, 'selected': False, 'text': '<p>Sorry guys I should have been more specific.  Whenever I added spaces the values were still being accepted.  The dollar sign <code>$</code> did the trick!</p>\n'}, {'answer_id': 19026, 'author': 'Robert Swisher', 'author_id': 1852, 'author_profile': 'https://Stackoverflow.com/users/1852', 'pm_score': 0, 'selected': False, 'text': '<p>A simpler way to write it still would be</p>\n\n<pre><code>var name_regex = /^([a-z0-9_])+$/i;\n</code></pre>\n'}, {'answer_id': 19341, 'author': 'travis', 'author_id': 1414, 'author_profile': 'https://Stackoverflow.com/users/1414', 'pm_score': 0, 'selected': False, 'text': '<p>Even simpler:</p>\n\n<pre><code>var name_regex = /^\\w+$/;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/18861', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/428190/']