Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

Can anyone know?

how to validate the textbox that should accept only Numeric values?
Posted

Hi, call JS function on textbox. use below code for this:

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


JS Function

JavaScript
function isNumericWithDecimal(e) 
{
    if ((e.which < 48 || e.which > 57)) 
    {
        if (e.which == 8 || e.which == 46 || e.which == 0) 
        {
            return true;
        }
        else 
        {
            return false;
        }
    }
}
 
Share this answer
 
Comments
Varun Sareen 20-Feb-12 1:24am    
My 5! good work sarvesh jee..nice code
JavaScript
Validate using javascript like,

function allownumbers(e)
   {
    var key = window.event ? e.keyCode : e.which;
    var keychar = String.fromCharCode(key);
    var reg = new RegExp("[0-9.]")
    if (key == 8)
    {
     keychar = String.fromCharCode(key);
    }
    if (key == 13)
    {
     key=8;
     keychar = String.fromCharCode(key);     
    }
    return reg.test(keychar);
   } 

Call this method in ONKEYPRESS event of the textbox like,
txtBox.Attributes.Add("onkeypress","javascript:return allownumbers(event);");


More options:

http://forums.asp.net/t/1156966.aspx/1[^]
 
Share this answer
 
VB
<asp:RegularExpressionValidator id="RegularExpressionValidator1"
                   ControlToValidate="TextBox1"
                   ValidationExpression="\d+"
                   Display="Static"
                   EnableClientScript="true"
                   ErrorMessage="Please enter numbers only"
                   runat="server"/>
 
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