Javascript Non Alphanumeric Characters Regex

Using javascript regular expressions to stop users from entering non-aphanumeric characters or white spaces

Say you want to stop users from entering non-aphanumeric characters or white spaces.

Using regular expressions would be the easiest method:

Here is my javascript code:

<script language="Javascript">
function alphaNumericCheck(){
var regex=/^[0-9A-Za-z]+$/; //^[a-zA-z]+$/
return true;
} else {
alert("Please fix: password")
return false;

For numbers only use /^[0-9]+$/

For mixed text and numbers, with spaces /^[0-9a-zA-Zs]+$/

Here are more useful regular expressions:

[a-zA-Z] any letter
d any number; same as [0-9]
D any NOT number; same as [^0-9]
w any alphanumeric character; same as [a-zA-Z-0-9_]
W any NON-alphanumeric character; same as [^a-zA-Z0-9_]
s any whitespace (tab, space, newline, etc...)
S any NON-whitespace
n newline
t tab

To view a full html sample of the above code click the links below using regular expressions in JavaScript

Tagged : / / / / / / / / / / / / / / / / /

9 thoughts on “Javascript Non Alphanumeric Characters Regex

  1. “For mixed text and numbers, with spaces /^[0-9a-zA-Zs]+$/”

    This does not work !

    Here is the correct one :


    BTW, thanks for your article that saved us bunch of hours !

  2. /\W/ is a nice shortcut, but it excludes the space.

    I used this for alphanumeric and space input only.


    function checkAlphanumeric(v) {
    var regex=/^[0-9a-zA-Zs’ ‘]+$/; //^[a-zA-z’ ‘]+$/
    return true;
    } else {
    return false;

    And for the form input onKeyUp=”return checkAlphanumeric(this)”


Leave a Reply

Your email address will not be published. Required fields are marked *