Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I only need to type numbers and one dot(.) in my textbox. how can i validate it. i am using asp.net with vb.

thanks
afsal
Posted

Try this:
C#
void NumberValidation(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 46)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
 
Share this answer
 
Comments
afsal.mp 21-Mar-12 6:02am    
where to fix this code?
afsal.mp 21-Mar-12 6:17am    
thank you pashad
Prasad_Kulkarni 21-Mar-12 8:03am    
Glad it helps
You can use Filter textbox extender of ajax control toolkit,or you can use javascript allow user type only what you want.
 
Share this answer
 
Comments
afsal.mp 21-Mar-12 6:16am    
done it


function ValDigit(val) {

var keyCodeEntered = (event.which) ? event.which : (window.event.keyCode) ? window.event.keyCode : -1;

if ((keyCodeEntered >= 48) && (keyCodeEntered <= 57)) {

return true;

}

else if (keyCodeEntered == 46) {

if ((val.value) && (val.value.indexOf('.') >= 0))

return false;

else

return true;

}



return false;

}


<asp:TextBox ID="TextBox1" onkeypress="return ValDigit(this);" runat="server">
Sarathkumar Nallathamby 19-Dec-12 7:52am    
I got the code using Ajax for numbers and dots.. But my Case is for only one dot have to enter and two decimals only allowed....!!!
try this

in textbox textchanged validate this function

C#
public static bool IsDesimal(string strValue)
        {
            string decimalNumber = ".0123456789";
            for (int i = 0; i < strValue.Trim().Length; i++)
            {
                if (decimalNumber.IndexOf(strValue[i]) == -1)
                {
                    return false;
                }
            }

            return true;
        }
 
Share this answer
 
v2
hi,
try the following,

XML
<HTML>
   <HEAD>
   <SCRIPT language=Javascript>
      <!--
      function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
 
         if (charCode = 46 || (charCode >= 48 && charCode <= 57))
            return false;

         return true;
      }
      //-->
   </SCRIPT>
   </HEAD>
   <BODY>
      <INPUT id="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar">
   </BODY>
</HTML>
 
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