Click here to Skip to main content
12,951,034 members (56,430 online)
Rate this:
 
Please Sign up or sign in to vote.
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 16-Apr-12 19:24pm
codeBegin16.1K
Updated 9-May-17 0:09am
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...
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

You can use Regular Expressions[^] to validate a textbox through Javascript[^]. You can use this site[^] to test your Regex patterns.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 7

For the server side you can use this way....

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>
  <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>
  Permalink  
v2
Comments
shaju.nair 15-Oct-12 10:46am
   
Good one ! thanks
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Try this:
void NumberValidation(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 46)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 13

validateInt() allows only the integer values to the textbox.

//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.

//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.
<asp:TextBox ID="txtDays" runat="server" onkeypress="return validateInt(event)" />
 

<asp:TextBox ID="txtCost" runat="server" onkeypress="return validateDec(event)" />
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

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 :)
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 11

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[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 5,984
CHill60 3,460
Maciej Los 3,053
Jochen Arndt 1,975
ppolymorphe 1,820


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 9 May 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100