Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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
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 at 15-Oct-12 10:46am
   
Good one ! thanks
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 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 Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 10

private void txtQuantity_KeyPress(object sender, KeyPressEventArgs e)
       {
           if (char.IsDigit(e.KeyChar) == true || char.IsControl(e.KeyChar) == true || e.KeyChar=='.')
           {
               if (Regex.IsMatch(txtQuantity.Text, @"\.\d\d\d"))
               {
                   if (e.KeyChar == (char)8)
                   {
                       //This makes backspace working
                   }
                   else
                   {
                       e.Handled = true;
                   }
               }
           }
           else
           {
               e.Handled = true;
           }
 
       }
 

This above code will allow your textbox to accept only digits with Dot(.) and Three Numbers after "." [eg. 24.222]
  Permalink  
Comments
Sandesh68 at 23-May-14 8:14am
   
you can increase digits after DOT '.' from increase no of "/D" and decrease with to decrease /d.
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  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 12

lprivate void TXT_Ac_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit(e.KeyChar)) e.Handled = true;
if (e.KeyChar == (char)8) e.Handled = false;
 
}
 
i,m ZAID Ansari Your Solution
  Permalink  
Comments
CHill(Hawkeye)60 at 21-May-14 17:33pm
   
2 years late. I don't believe you tested this either
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 4

  Permalink  

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



Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 10 Jul 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid