Click here to Skip to main content
14,422,272 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi Sir

how to write regular expression for text box should not accept spaces and should not accept alpha....
Posted
Comments
   
Did you simply read Regex reference or tutorial? The problem is too simple.
--SA

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 2

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>
   <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
   

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




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