textbox allow character validation

10/10/2011 18:24

 public static void NumericOnly(KeyPressEventArgs e)
        {
            try
            {
                string allowedChars = "0123456789";

                if (allowedChars.IndexOf(e.KeyChar) == -1)
                {
                    // Invalid Character
                    e.Handled = true;
                }
            }
            catch
            {

            }
        }

        public static void DecimalOnly(KeyPressEventArgs e)
        {
            try
            {
                string allowedChars = "0123456789.";

                if (allowedChars.IndexOf(e.KeyChar) == -1)
                {
                    // Invalid Character
                    e.Handled = true;
                }
            }
            catch
            {

            }
        }

        public static void DateOnly(KeyPressEventArgs e)
        {
            try
            {
                string allowedChars = "0123456789/";

                if (allowedChars.IndexOf(e.KeyChar) == -1)
                {
                    // Invalid Character
                    e.Handled = true;
                }
            }
            catch
            {

            }
        }

        public static void StringOnly(string allowedChars, KeyPressEventArgs e)
        {
            try
            {

                if (!char.IsLetter(e.KeyChar) || allowedChars.IndexOf(e.KeyChar) == -1)
                {
                    e.Handled = true;
                }
            }
            catch
            {

            }
        }
        //Mahadevan
        public static void OnlyDecimal(KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }
        }

        public static void OnlyDigitsBackspace(KeyPressEventArgs e)
        {
            char keyChar;
            keyChar = e.KeyChar;

            if (!Char.IsDigit(keyChar)      // A - Z,a-z
               &&
               keyChar != 8               // backspace
               &&
               keyChar != 32              // space bar
                 &&
               keyChar != 13
               )
            {
                //  Do not display the keystroke
                e.Handled = true;
            }
            else
            {
                e.Handled = false;
            }

        }
        public static void OnlyCharBackspace(KeyPressEventArgs e)
        {
            char keyChar;
            keyChar = e.KeyChar;

            if (!Char.IsLetter(keyChar)      // A - Z,a-z
               &&
               keyChar != 8               // backspace
               &&
               keyChar != 32              // space bar
                 &&
               keyChar != 13
               )
            {
                //  Do not display the keystroke
                e.Handled = true;
            }
            else
            {
                e.Handled = false;
            }

        }