Click here to Skip to main content
14,880,436 members
Please Sign up or sign in to vote.
1.28/5 (4 votes)
See more:
I only need to type numbers and one dot(.) in my text box. How can i validate it using code. Not by asp controls.
Posted
Updated 21-Feb-19 20:55pm
Comments
Philippe Mori 16-Nov-16 9:18am
   
And you have to consider that in some language, a comma is used as the decimal separator...

You can use Regular Expressions[^] to validate a textbox through Javascript[^]. You can use this site[^] to test your Regex patterns.
   
For the server side you can use this way....

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




You can use this javascript function for the client side validation.

HTML
<html>
  <head>
    <script language="Javascript">
       <!--
       function isNumberKey(evt)
       {
          var charCode = (evt.which) ? evt.which : event.keyCode
          if (charCode != 46 && 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>
   
v2
Comments
shaju.nair 15-Oct-12 10:46am
   
Good one ! thanks
   
Text box accept only numbers instead of letters or special characters

<input type="text" name="search" id="txtSearch" class=" search-query form-control" placeholder="Search by Id" onkeypress="return event.charCode >= 48 && event.charCode <= 57" onpast="return false" />
   
v2
Comments
CHill60 26-Feb-18 7:52am
   
The question was asked and answered nearly 6 years ago! Stick to answering new questions where the OP still needs help. Also make sure you have read, understood and are answering, the question that was asked. This code will not allow any decimal points never mind ensuring only one is entered
u can do that using javascript fuction .make a java script function in head tag and call that function in textbox change event or or set the autopostback propertie to true of textbox.
   
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;
            }
        }
   
Comments
Jai panchal 17-Feb-18 3:05am
   
whats the namespace for this " keyevent args " ??
validateInt() allows only the integer values to the textbox.

C#
//Function to allow only Integer values to textbox
        function validateInt(key) {
            //getting key code of pressed key
            var keycode = (key.which) ? key.which : key.keyCode;
            //comparing pressed keycodes
            if ((keycode != 8) && (keycode < 48 || keycode > 57)) {
                return false;
            }
            else {
                return true;
            }
}


validateDec() allows Decimal values and it allows only one decimal point.

C#
//Function to allow only Decimal values to textbox
function validateDec(key) {
            //getting key code of pressed key
            var keycode = (key.which) ? key.which : key.keyCode;
            //comparing pressed keycodes
            if (!(keycode == 8 || keycode == 46) && (keycode < 48 || keycode > 57)) {
                return false;
            }
            else {
        var parts = key.srcElement.value.split('.');
                if (parts.length > 1 && keycode == 46)
                    return false;
                return true;
            }
}


And call the above required method from your textbox keypress event like below.
C#
<asp:TextBox ID="txtDays" runat="server" onkeypress="return validateInt(event)" />


<asp:TextBox ID="txtCost" runat="server" onkeypress="return validateDec(event)" />
   
v2
You can do it using jquery or native javascript.

Try these:

Text box to accept only number[^]

Restricting input to textbox: allowing only numbers and decimal point[^]

hope it helps :)
   
There is a way suggested as 'best practice' and that is using regex validation...
Read here - with samples - http://msdn.microsoft.com/en-us/library/ff650303.aspx[^]
   

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