0

I am new to programming so I hope you keep an open mind. I am using web forms in visual studio 2013. the language I am using is C#. I have a textbox which I want to accept only digits and one decimal point. How can I do that.

note: I've seen lots of code but most of them are using the KeyPress event which I assume is not valid for web forms because when I go to the events section in the property window of my textbox I only see one event which is TextChanged.

any tips will help I really need this.

thanks in advance.

user3419838
  • 3
  • 1
  • 2

5 Answers5

3

You can use JavaScript for the purpose. Using javascript will restrict users from entering all the characters other than numbers and decimal point. Include the following Javascript in the head section of your aspx page.

<SCRIPT language=Javascript>
    function isDecimal(evt)
    {
       var charCode = (evt.which) ? evt.which : event.keyCode
       var parts = evt.srcElement.value.split('.');
       if(parts.length > 1 && charCode==46)
          return false;
       else
       {
          if (charCode == 46 || (charCode >= 48 && charCode <= 57))
             return true;
          return false;
       }
    }
</SCRIPT>

And in the textbox control include the following snippet

onkeypress="return isDecimal(event)"

Sajeel
  • 148
  • 13
1

You could write a regex validator with the following expression: ^\d+(\.\d+)?$ this should make sure that you have numbers such as 100 or 11220.22. An explanation of the regex is available here.

EDIT: As per your comment, the problem is that the \ is a special character also in C# and thus needs to be escaped in your code behind classes, so in your case, the expression would look like so: ^\\d+(\\.\\d+)?$.

I think that a better approach, though, would be to use a Regular Expression Validator as shown here. This should validate the text only once (upon pressing submit). The way you built it (in the code behind) can be a bit heavy since it will most likely fire a server side event each time the text is changed. Also, I do not think (if memory serves) that you need to escape the regex if you go with an ASP.NET validator.

EDIT 2: This regex should help: ^[1-9]\\d*(\\.\\d+)?$

npinti
  • 50,175
  • 5
  • 67
  • 92
  • Thanks it worked for me but now there's another issue I want the value entered to be greater than zero. Meaning the user shouldn't enter negative values or 0 or something like 000. – user3419838 Mar 14 '14 at 15:35
0

Take a look at this thread, it describes several approaches: http://bytes.com/topic/net/answers/562094-restrict-user-enter-numeric-value-textbox

I believe like npinti already mentioned, validators would be one approach. What you have in mind might require a JavaScript approach, there is one shown in the linked article (at the bottom).

TheUser1024
  • 101
  • 1
0

You can use a validator with target set to your text box and with regex \d+(.\d)?

For more information, check here Simple regular expression for a decimal with a precision of 2

Community
  • 1
  • 1
0

Another example ,

 private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
{

    if (txtPrice.Text.Length == 0)
    {
        if (e.KeyChar == '.')
        {
            e.Handled = true;
        }
    }
    if (!char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 46)
    {
        e.Handled = true;
    }
    if (e.KeyChar == '.' && txtPrice.Text.IndexOf('.') > -1)
    {
        e.Handled = true;
    }
}
Sherif Hamdy
  • 569
  • 6
  • 10