Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
Hi Sir

how to write regular expression for text box should not accept spaces and should not accept alpha....
Posted
Comments
Sergey Alexandrovich Kryukov 7-Aug-12 12:26pm    
Did you simply read Regex reference or tutorial? The problem is too simple.
--SA

1 solution

You can make a Regular Expression for that of course, and that is very simple, but in your particular case, very simple one, it's much better to filter out unwanted keystrokes while the user types the text.

This is a simple code sample:
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 for the character #8. This is a backspace, which you should allow for input specifically. You can modify this code to allow characters of your choice.

—SA
 
Share this answer
 

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