Masking Textbox to accept only decimals

给你一囗甜甜゛ 提交于 2019-12-02 09:35:19

Modify your regex with this:

^\d+([\.\d].{1,2})?$

DEMO

EDIT:

The above regex will also allow 123..1 that is more than 1 decimal point. so I just found the problem and fixed with the below one:

^(\d+)?+([\.]{1})?+([\d]{1,2})?$

DEMO

Either you can use the Regular Expression

^(\d+)?+([\.]{1})?+([\d]{1,2})?$

Or you can use below event

   bool blHasDot = false;
   private void txt_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (Char.IsDigit(e.KeyChar) || e.KeyChar == '\b')
        {
            // Allow Digits and BackSpace char
        }
        else if (e.KeyChar == '.' && !blHasDot)
        {
            //Allows only one Dot Char
            blHasDot=true;
        }
        else
        {
            e.Handled = true;
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!