Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
->Textbox should not allow Null value & it allow Double values
->It does not allow any string values.
Posted

Use Regular Expression

^[0-9]([.,][0-9]{1,3})?$ it allow only numbers
 
Share this answer
 
 
Share this answer
 
Hi,

As you are in windows use this : http://msdn.microsoft.com/en-us/library/vstudio/kkx4h3az(v=vs.100).aspx[^]
it will take only numeric not strings

To eliminate null or blank you can use : Validate user input in Windows Forms[^]
 
Share this answer
 
// this code are called in textbox keypress event
public void IntegerOnly(object sender, KeyPressEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8 & e.KeyChar != 13 & e.KeyChar != 46) && (e.KeyChar != 22) && (e.KeyChar != 3))
            {
                e.Handled = true;
            }
            else
            {
                if (tb.Text.Length > 9 && e.KeyChar != 8 && e.KeyChar != 13 && e.KeyChar != 46)
                {
                    e.Handled = true;
                }
            }
        }



        public void TwoDecimalOnly(object sender, KeyPressEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8 & e.KeyChar != 13 & e.KeyChar != 46) && (e.KeyChar != 22) && (e.KeyChar != 3))
            {
                e.Handled = true;
            }
            if (tb.Text.Contains("."))
            {
                if (e.KeyChar == 46)
                    e.Handled = true;
                int dot = tb.Text.IndexOf(".");
                if ((tb.Text.Substring(dot).Length > 2 && e.KeyChar != 8 && e.KeyChar != 13))
                    e.Handled = true;
            }
            else
            {
                if (tb.Text.Length > 9 && e.KeyChar != 8 && e.KeyChar != 13 && e.KeyChar != 46)
                {
                    e.Handled = true;
                }
            }
        }


        public void ThreeDecimalOnly(object sender, KeyPressEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8 & e.KeyChar != 13 & e.KeyChar != 46) && (e.KeyChar != 22) && (e.KeyChar != 3))
            {
                e.Handled = true;
            }
            if (tb.Text.Contains("."))
            {
                if (e.KeyChar == 46)
                    e.Handled = true;
                int dot = tb.Text.IndexOf(".");
                if ((tb.Text.Substring(dot).Length > 3 && e.KeyChar != 8 && e.KeyChar != 13))
                    e.Handled = true;
            }
            else
            {
                if (tb.Text.Length > 9 && e.KeyChar != 8 && e.KeyChar != 13 && e.KeyChar != 46)
                {
                    e.Handled = true;
                }
            }
        }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900