如何限制文本框接受十进制数只有一个点在重点preSS事件在C# [英] How to restrict the TextBox to accept only one dot in decimal number in keypress event in C#

查看:135
本文介绍了如何限制文本框接受十进制数只有一个点在重点preSS事件在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发Windows手机应用程序,在此我想限制asp.net文本框接受十进制数(C#)只有一个点,所以请建议我如何做到这一点。

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.

先谢谢了。

推荐答案

我只想注册到的文本框框TextChanged 事件。为了验证一个十进制数,你既可以使用基本的<一个href=\"http://stackoverflow.com/questions/308122/simple-regular-ex$p$pssion-for-decimal-numbers\">regex或 Decimal.TryParse 方法。这两种方法如下所示。

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

这篇关于如何限制文本框接受十进制数只有一个点在重点preSS事件在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆