1

I am developing a windows mobile application, in this i want to restrict the asp.net textbox to accept only one dot in decimal number (C#) so please suggest me how to do this.

Thanks in advance.

Eli
  • 17,397
  • 4
  • 36
  • 49
sreenu
  • 443
  • 5
  • 9
  • 16

2 Answers2

1

I would just register to the Textbox TextChanged event. To validate a decimal number, you could either use a basic regex or Decimal.TryParse method. Both methods are shown below.

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
   if(!Regex.IsMatch(TextBox1.Text, @"[0-9]+(\.[0-9][0-9]?)?"))
      TextBox1.BackColor = Color.Red;

   decimal value;
   if(!decimal.TryParse(TextBox1.Text, out value))
      TextBox1.BackColor = Color.Red;
}
Community
  • 1
  • 1
ahawker
  • 3,306
  • 24
  • 23
1

One way to do it is with control extenders. You can

  1. Check client side via ajax/javascript to be sure only one decimal is entered, and

  2. You can reuse the extender on other textboxes on future forms.

Chuck Savage
  • 11,775
  • 6
  • 49
  • 69