C validating numeric input
Regular Expressions is a huge topic that I cannot hope to cover in a short article.
However for the sake of simplicity, regular expressions are string combinations that are used to quickly match specific patterns in a body of text.
Unlike a user’s name, which is variable-length and where the validation criteria are the same for every character, a phone number is a fixed length but the validation criteria differ depending on the position of the character.
The template will work as follows: A # will match any digit in the user input. So, if we ask the function to match the template “(###) ###-####”, that means we expect the user to enter a ‘(‘ character, three numbers, a ‘)’ character, a space, three numbers, a dash, and four more numbers.
With the C# event, this task is relatively simple and does not require a custom C# control.
We will look over two common methods that are somewhat limited, and a better alternative at the end (if you are in a hurry, skip to the textbox that handles important non-integer keys). However if we want to write more efficient C# code, it is better and faster to use Regular Expressions.
The one we need is The two methods above have a significant limitation, they suppress keystrokes that are not numbers but are perfectly valid, for example Backspace, Delete, Ctrl C, etc.
Writing an exception case for each of these would be rather inefficient.
The following functions live in the cctype header: String validation Let’s do a simple case of string validation by asking the user to enter their name.