Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why the result of IsDigit is not as intended

In the below code if I enter numbers then it flashes error message instead it should flash in case of alphabets. I want to allow only numbers.

Also if I enter number and then any alphabet for e.g. 33er then the error msg is displayed and unless and untill I do not remove whole text the msg flashes, I have remove whole text and then I have enter numbers.

if (e.Handled != char.IsDigit(e.KeyChar))
{
   errorProvider1.SetError(label1, "Allow Only Numeric Values !");
   label1.Text = "Allow Only Numeric Values !";
}
else
{
   errorProvider1.SetError(label1, "");
   label1.Text = "";
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I suggest not checking e.Handled but set it to true when required

if (char.IsDigit(e.KeyChar)) {
  // Expected input, clear error messages
  errorProvider1.SetError(label1, "");
  label1.Text = "";
}
else {
  // Do not accept user input
  e.Handled = true;

  // Show error messages
  errorProvider1.SetError(label1, "Allow Only Numeric Values !");
  label1.Text = "Allow Only Numeric Values !";
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading