vijay rags

vijay rags

  • NA
  • 68
  • 163.4k

How to validate textbox on key down event to accept only numbers in silverlight 4

Nov 19 2011 9:02 AM

Hi,

 i need a validation to check whether key pressed down is numeric or not. i tried with different code but they cant help me out.In the textbox if the user press Shift+numbers it displays special charcters like !,@,#...... I need to validate the Shift + key down event. Please find the sample code i tried below and help me out.


 private void txtNumericTextbox_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key < Key.D0 || e.Key > Key.D9)
                {
                    if (e.Key < Key.NumPad0 || e.Key > Key.NumPad9)
                    {
                        if (e.Key != Key.Back)
                        {
                            txtNumericTextbox_.BorderBrush = new SolidColorBrush(Colors.Red);
                            lblErrorMessage.Visibility = Visibility.Visible;
                            lblErrorMessage.Text = "Please Enter Numbers Only";
                        }
                        else
                        {
                            txtNumericTextbox_.BorderBrush = new SolidColorBrush(Colors.DarkGray);
                            lblErrorMessage.Visibility = Visibility.Collapsed;
                            lblErrorMessage.Text = "";
                        }
                    }
                }


Thanks,
Vijay       

Answers (7)