If you are not using server controls (which means you cannot use the validation controls) or if you need to validate input from sources other than form fields, such as query string parameters or cookies, you can use the Regex class within the System. The following example shows how to use a regular expression to validate a name input through a regular client-side HTML control.
Regular expressions are much easier to understand if you use the following syntax and comment each component of the expression by using a number sign (#).
The 'Not very precise:' only match against the first and second digit so may allow invaid numbers.
This also works if you are trying to omit other words, names etc.
To avoid such vulnerability, you should validate text fields (such as names, addresses, tax identification numbers, and so on) and use regular expressions to do the following: Regular expression support is available to ASP. If you capture input by using server controls, you can use the Regular Expression Validator control to validate that input.
NET applications through the Regular Expression Validator control and the Regex class in the System. You can use regular expressions to restrict the range of valid characters, to strip unwanted characters, and to perform length and format checks.
It is provided as a courtesy for individuals who are still using these technologies.
Some of the possible options are 12 hour or 24 hour, with seconds or without.
You can constrain the input format by defining patterns that the input must match.
To validate a server control's input using a Regular Expression Validator The regular expression used in the preceding code example constrains an input name field to alphabetic characters (lowercase and uppercase), space characters, the single quotation mark (or apostrophe) for names such as O'Dell, and the period or dot character.
This How To shows how you can use regular expressions within ASP. Regular expressions are a good way to validate text fields such as names, addresses, phone numbers, and other user information.
You can use them to constrain input, apply formatting rules, and check lengths. This How To shows how you can use regular expressions within ASP. Objectives Overview Using a Regular Expression Validator Control Using the Regex Class Common Regular Expressions Additional Resources If you make unfounded assumptions about the type, length, format, or range of input, your application is unlikely to be robust.