Validating a text field with a regular expression that does not allow decimal

I wanted to create a TextChanged event that will check whether the input text matches specific criteria and delete the last character entered if it didn't. In this case, the criteria are numbers, 1 decimal and 1 fraction.

I tested the regex only for numbers and decimal numbers and ran into a problem. I tried several different expressions (Iโ€™m terrible to write my own, so they are chosen from different questions), and each result is the same. It accepts numbers just fine, but does not accept decimals. Any help would be greatly appreciated!

string isNumber = @"^\d{1,9}(\.\d{1,9})?$"; private void TextBox_TextChanged(object sender, EventArgs e) { TextBox text = (TextBox)sender; Match match = Regex.Match(text.Text, isNumber); if (!match.Success) { if (text.Text.Length > 1) text.Text = text.Text.Substring(0, text.Text.Length - 1); else text.Text = ""; text.Select(text.Text.Length, 0); //set cursor to the end //of the string } } 
+4
source share
3 answers

I think the problem is that you are trying to check the number of characters by character as user types, but it is not possible to enter a valid decimal number that temporarily does not have an invalid value.

Consider what happens when a user enters a value of 1.2 :

  • User enters character 1 .
  • Validation is triggered due to a change in the value of the text field.
  • 1 is a valid decimal, so verification passes.
  • The user continues to type by adding a decimal point character .
  • The check fires again because the value of the text field has changed.
  • 1. not a valid decimal according to the regular expression, so the last character is erased.
  • The text box value now returns to 1 .
  • The user is upset. Go to step 4.

As you can see, entering a decimal point is not possible. But if you try to change your regular expression so that the decimal point exists without the next digit, then you may have an invalid value if the user stops after entering the decimal point and then submits the form.

So, the point is that using a validation scheme for each character will not work for decimal places. A better approach would be to let the user enter whatever they want in the field, and then check later when the user finishes typing. You can do this by checking when the text field loses focus or when the form is submitted.

+6
source

This is not the most beautiful, but it seems to do what you need:

 ^(\d{1,9}\.|)\d{1,9}(\.(\d{1,9})?)$ 

I should note that this will not work for a number starting with a decimal number.

+1
source

One of the problems, at least, is related to what (and how) you are trying to do (for example, Brian indicated so well), you probably mean what allows the first 3 characters of the text field to be in the format "1.3" , for example, and removing char if it is not correct in this position. If you really intended to do this, you can do it (I did not post any code for the case if the user places another character after success, in this case the fourth char):

  string isNumber = @"^[1-9]{1}\.[1-9]{1}$"; private void textBox1_TextChanged(object sender, EventArgs e) { TextBox text = (TextBox)sender; Match match; switch (text.Text.Length) { case 1: if (char.IsDigit(text.Text[0])) break; else text.Text = ""; break; case 2: if (char.IsPunctuation(text.Text[1])) break; else { text.Text = text.Text.Remove(text.Text.Length - 1, 1); text.Select(text.Text.Length, 0); } break; case 3: match = Regex.Match(text.Text, isNumber); if (!match.Success) { text.Text = text.Text.Remove(text.Text.Length - 1); text.Select(text.Text.Length, 0); } else MessageBox.Show("Success!!!!!"); break; } } 

As you can see, I do char with char until it reaches 3 in length, then I will check the regex.

+1
source

Source: https://habr.com/ru/post/1492800/


All Articles