Sunday, April 19, 2009

Regex - String input validation by regular expressions

Using regular expression is one of the most efficient ways to bring security to validate user input. As an example, the following regular expression works to match valid names:

[a-zA-Z'-‘Ãâå\s]{1,40}
...
using System.Text.RegularExpressions;
...
Regex.IsMatch(s, @"^[a-zA-Z'-‘Ãâå\s]{1,40}$" )
...

Generally most input validation should be pessimistic and allow only input that consists entirely of approved characters. In this way, may user encounter with some restrictions but it helps to protect against malicious input such as SQL injection attacks.
Share/Bookmark

No comments: