Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: Javascript
Hi All
i need a javascript code for not allowing special characters and numbers and allow
spaces in my text box
thanks in advance
Posted 27-Jun-12 20:37pm
Sergey Alexandrovich Kryukov at 28-Jun-12 1:56am
What are those "special characters"? There is no such thing.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This article contains a small JS framework which contains what you need. also many more of similar nature.
A Tiny Javascript Framework for Common Validation Scenarios.[^]
P.S. just use the AcceptAlphabetOnly function with argument for spaces as true.
Comments at 28-Jun-12 1:50am
thanks rahul .. this solution solved my problem
Rahul Rajat Singh at 28-Jun-12 1:55am
you are welcome. if it worked then please mark the answer as solution as this would let other know that the problems is solved and the others with similar problem can also refer and benefit from it. at 28-Jun-12 2:07am
done it
Rahul Rajat Singh at 28-Jun-12 2:09am
thanks. at 28-Jun-12 2:44am
hi .. if u dontmind let me know how to allow dot(.) in this javascript at 28-Jun-12 2:45am
i got it thanks
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

This is a simple sample of similar thing:
Here is how to filter out:
      <script type="text/javascript"><!--
         function filterDigits(eventInstance) { 
            eventInstance = eventInstance || window.event;
                key = eventInstance.keyCode || eventInstance.which;
            if ((47 < key) && (key < 58) || key = 45 || key == 8) {
               return true;
            } else {
                    if (eventInstance.preventDefault) eventInstance.preventDefault();
                    eventInstance.returnValue = false;
                    return false;
            } //if
         } //filterDigits
<input type="text" onkeypress="filterDigits(event)"/>
Pay attention that the key code 8 (backspace) is allowed. By some historical reason, this key is considered as a character, so it should be specifically allowed through the filter.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,580
1 OriginalGriff 8,370
2 Peter Leow 4,859
3 Kornfeld Eliyahu Peter 3,210
4 Maciej Los 2,301

Advertise | Privacy | Mobile
Web02 | 2.8.150327.1 | Last Updated 28 Jun 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100