A post over at the ASP.NET forums reminded me of an odd 'feature' that the RegularExpressionValidator has - namely that it will validate to false if your regular expression pattern only matches part of the input - even though that may very well constitute a successful match.
Say, for instance, that you have an email input field and wish to do a very* naive validation of it with the expression @, which basically states that you expect the input to have an @ character somewhere within it:
<asp:TextBox
ID="_emailTextBox"
runat="server" />
<asp:RegularExpressionValidator
ID="_emailTextBoxValidator"
runat="server"
ValidationExpression="@"
ControlToValidate="_emailTextBox"
ErrorMessage="Invalid email!" />
But that won't work:
If you use the very same expression with the Regex class, however:
if (Regex.Match(_emailTextBox.Text, "@").Success)
{
// valid!
}
... then the input 'test@test.com' will validate to true, as expected. Now of course there's an easy fix here - change the expression to ^.*@.*$ ("match any number of characters from the beginning of the string until an @, and then any number of characters until the end of the string") and it will do what you want in both cases. But unless you know about it, its easy to spend awhile scratching your head over this one...
* Email address validation is very complex these days (because of a very complex spefication), and although I would recommend a 'loose' validation expression so that you don't end up blocking valid email addresses, the one in this example is perhaps a bit too simple :p