Need an input field that will only allow letters or numbers - no symbols

Posted on 2007-09-29
Last Modified: 2013-11-19
I have an input field which I would like to limit what people can enter to just letters or numbers. I have a similar function that only allows numbers. I just adapted it from a snippet I found online. Here's an example of its use:

<input type="text" name="numbers" onKeyUp="this.value=this.value.replace(/\D/,'');" />

It seems like it shouldn't be hard to come up with something similar by just replacing the expression, but I don't know much about using regular expressions in javascript. I thought maybe using this:
"this.value=this.value.replace(/(?![0-9a-zA-Z])/,'');" would work, but it doesn't - no characters get replaced.

I can't simply use "\W" in place of "\D" because it matches the underscore character, which I don't want.
Question by:MasonWolf
    LVL 28

    Accepted Solution


    Hi, what about
    <input type="text" name="numbers" onKeyUp="this.value=this.value.replace(/[\W_]/,'');" />
    LVL 6

    Expert Comment

    by:C TG
    If you want to exclude the _ (underscore) too, you just need to add this near the \W like in the bellow example:

    <input type="text" name="numbersandletters" onKeyUp="this.value=this.value.replace(\W_*,'');" />

    If you can't simply use the " \W ".... or if you want to exclude only a custom set of special chars, than you can make a enumeration of the excluded chars like:
    <input type="text" name="some excludes" onKeyUp="this.value=this.value.replace('!{0,}@{0,}#{0,}\${0,}%{0,}\^{0,}\&{0,}\*{0,}\({0,}\){0,}\-{0,}={0,}\+{0,}\\{0,}\|{0,}\]{0,}\}{0,}\{{0,}\[{0,}','');" />

     ...whit the note that the special chars must be escaped by the " \ " character .. and the {0,} means that any of those chars must have 0 or more occurrence and may also be replaced by the " * " char like in the bellow example:

    <input type="text" name="some excludes" onKeyUp="this.value=this.value.replace('!*@*#*\$*%*\^*\&*\**\(*\)*\-*=*\+*\\*\|*\]*\}*\{*\[*','');" />

    This is the most simple solution I can offer to you. Please ask, if this isn't waht youre looking for.
    LVL 13

    Author Comment

    Thanks TName! I thought it was something simple like that - just couldn't seem to get it on my own.

    thanks for trying, but your solution didn't actually work when I tested it

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
    Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now