19

I want to check if a text box input is valid (only alphabet, numbers and underscores allowed. No whitespaces or dashes). I currently have this, but whitespaces & dashes seem to pass.

function validText(field)
{
    var re = /[a-zA-Z0-9\-\_]$/
    if (field.value.search(re) == -1)
    {
        alert ("Invalid Text");
        return false;
    }
}

A valid input would be something like

'Valid_Input123'

invalid

'Invalid-Input !'
3
  • 6
    Tip: [a-zA-Z0-9_] == \w
    – elclanrs
    Feb 4, 2014 at 0:21
  • 3
    Well, maybe dashes pass because you included a dash character. Feb 4, 2014 at 0:27
  • it is not a dash character, it means all letters from a to z Mar 30, 2020 at 14:59

4 Answers 4

35
  1. The \w is a handy regex escape sequence that covers letters, numbers and the underscore character
  2. You should test the entire string for valid characters by anchoring the validity test at the start (^) and end ($) of the expression
  3. The regular expression test method is faster than the string search method
  4. You can also test for one or more characters using the + quantifier

To summarise (in code)

var re = /^\w+$/;
if (!re.test(field.value)) {
    alert('Invalid Text');
    return false;
}
return true;

Alternatively, you can test for any invalid characters using

/\W/.test(field.value)

\W being any character other than letters, numbers or the underscore character.

Then you might also need to add a length check to invalidate empty strings, eg

if (/\W/.test(field.value) || field.value.length === 0)
7
  • OP also specified a hyphen in his character class Feb 4, 2014 at 0:25
  • @VasiliSyrakis OP also says (and I quote) "only alphabet, numbers and underscores allowed. No whitespaces or dashes"
    – Phil
    Feb 4, 2014 at 0:26
  • @VasiliSyrakis And yet that goes against the rest of the post: only alphabet, numbers and underscores allowed. No whitespaces or dashes
    – Ian
    Feb 4, 2014 at 0:26
  • 3
    @Phil This answer would be better with an explanation.
    – Daedalus
    Feb 4, 2014 at 0:26
  • @Daedalus I agree, but Felix types faster than I do
    – Phil
    Feb 4, 2014 at 0:26
13

You are only testing whether the text ends ($) with one of the characters in the character class. You are also explicitly allowing a dash (\-). If you don't want that, remove it.

Anchor the expression (^, $), add a quantifier (+) and .test whether the string only consists of those characters:

var re = /^[a-zA-Z0-9_]+$/; // or /^\w+$/ as mentioned
if (!re.test(field.value)) {

}
4
  1. You forgot to anchor your regex at the beginning using ^
  2. test is easier to use
  3. There is no need for the dash.

It should look like this:

if (!/^[a-z0-9_]+$/i.test(field.value)) {
    //
}
4

[\w]* will suffice.

Regex101 Example


This is a very basic Regular Expressions question

Learn more about regular expressions here: regular-expressions.info

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.