Click here to Skip to main content
15,886,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text box which should should only allow numbers between 0 to 9 and single decimal point(.) and only positive numbers.
Posted

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

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

         return true;
      }
 
Share this answer
 
Hi,

I hope this link[^] and this one[^] would help you a bit

Regards,
RK
 
Share this answer
 
You can do it on text box key press event or you can use ajax control (filter text).
Below i saw you how to do that on key press event :

C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar)
        && !char.IsDigit(e.KeyChar)
        && e.KeyChar != '.')
    {
        e.Handled = true;
    }

    // only allow one decimal point
    if (e.KeyChar == '.'
        && (sender as TextBox).Text.IndexOf('.') > -1)
    {
        e.Handled = true;
    }
}


For more detail refer this link :
http://stackoverflow.com/questions/463299/how-do-i-make-a-textbox-that-only-accepts-numbers[^]

Accept as answer or vote if help.
 
Share this answer
 
Use AJAX FilteredTextBoxExtender Control[^] like this:
C#
<asp:textbox id="txt" runat="server" ></asp:textbox>                   
 
<asp:filteredtextboxextender id="FilteredTextBoxExtender1" runat="server" filtertype="Numbers, Custom" validchars="." targetcontrolid="txt" ></asp:filteredtextboxextender>

Regards...
 
Share this answer
 
v3

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