Click here to Skip to main content
15,883,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All

i need a javascript code for not allowing special characters and numbers and allow
spaces in my text box

thanks in advance
afsal
Posted
Comments
Sergey Alexandrovich Kryukov 28-Jun-12 1:56am    
What are those "special characters"? There is no such thing.
--SA

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.
 
Share this answer
 
v2
Comments
afsal.mp 28-Jun-12 1:50am    
thanks rahul .. this solution solved my problem
Rahul Rajat Singh 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.
afsal.mp 28-Jun-12 2:07am    
done it
Rahul Rajat Singh 28-Jun-12 2:09am    
thanks.
afsal.mp 28-Jun-12 2:44am    
hi .. if u dontmind let me know how to allow dot(.) in this javascript
This is a simple sample of similar thing:
Here is how to filter out:
HTML
<html>
   <head>
      <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
      --></script>
   </head>
<body">

<input type="text" onkeypress="filterDigits(event)"/>

</body>
</html>


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.

—SA
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900