Click here to Skip to main content
11,801,910 members (57,795 online)
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 19: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 OriginalGriff 445
1 KrunalRohit 301
2 F-ES Sitecore 215
3 CHill60 185
4 Afzaal Ahmad Zeeshan 180
0 OriginalGriff 2,715
1 Maciej Los 1,900
2 KrunalRohit 1,797
3 CPallini 1,550
4 Richard MacCutchan 1,157

Advertise | Privacy | Mobile
Web02 | 2.8.151002.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