[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2007-09-29
3
Medium Priority
?
3,281 Views
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.
0
Comment
Question by:MasonWolf
3 Comments
 
LVL 28

Accepted Solution

by:
TName earned 1616 total points
ID: 19984191

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

Expert Comment

by:C TG
ID: 19984234
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.
0
 
LVL 13

Author Comment

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

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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article discusses how to implement server side field validation and display customized error messages to the client.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

834 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