Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,
i have a text box which i want to accept numbers only and another one which accepts letters only.
i have searched a lot and writing code , but all of them after leaving text box.
i want a solution during writing in text box.

Any Help , please

Thanks
Posted
Comments
Kiirrii 27-Feb-12 2:35am    
do u mean textbox which takes only no's den use ajax filtered textbox and provide no's only in properties

Hi, use below code:


ASP.NET
<asp:textbox id="txtBox" runat="server" onkeypress="return isNumeric(event);" xmlns:asp="#unknown"></asp:textbox>


JavaScript
function isNumeric(evt) {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if ((charCode < 48 || charCode > 57))
            return false;
 else if ((charCode == 8 || charCode == 46 || charCode == 0 ))
return true
else
        return true;
    }


It will accept decimal values also.

For only alphabets:

JavaScript
function isAlphabet(evt) {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if ((charCode < 123 && charCode > 96))
{
            return true;
}
else if ((charCode < 91 && charCode > 64))
{
return true
}
else if(charCode == 0 || charCode == 8)
{
return true
}
else
{
        return false;
}
    }
 
Share this answer
 
v3
Comments
CRDave1988 27-Feb-12 3:00am    
my 5
Try This:

JavaScript Code:

JavaScript
function isNumberKey(evt) {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

        return true;
    }


Design Code:

ASP.NET
<asp:textbox id="txtNoOfPer" runat="server" onkeypress="return isNumberKey(event);" xmlns:asp="#unknown"></asp:textbox>
 
Share this answer
 
use this code -------------
this code allowing to user only numeric values.......


HTML
<html>
   <head>
   <script language="Javascript">
     
      function isNumberKey(evt)
      {
        var charCode = (evt.which) ? evt.which :   event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
      }
      
   </script>
   </head>
   <body>
      <input id="txtChar"  önkeypress="return isNumberKey(event)" type="text" name="txtChar">
   </input></body>
</html>
 
Share this answer
 
v2
Thank You alot , it is working noooooooooooooooow
 
Share this answer
 
Comments
BillW33 24-Jul-12 16:45pm    
You should not post a comment to a solution as a "Solution". Instead add a comment to a previous solution by pressing the "Have a Question or Comment?" button.

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