I’m surely missing something here, as what I expect to work identically, is working differently.
Suppose the following RegularExpressionValidator syntax
<asp:RegularExpressionValidator runat="server" ID="rxEmail" ControlToValidate="txEmail"
ValidationExpression="<%$ appsettings:rxEmail %>" Text="*" />
and the following CustomValidator syntax/code
<asp:CustomValidator ID="cvEmail" runat="server" Text="*" onServerValidate="validateContactFormat" />
Sub validateContactFormat(ByVal sender As Object, ByVal args As ServerValidateEventArgs) Handles cvContactFormat.ServerValidate
Dim emailRegEx As Regex = New Regex(ConfigurationManager.AppSettings("rxEmail"))
args.IsValid = emailRegEx.IsMatch(txEmail.Text)
End Sub
The expression held in web.config is
[\w-]+@([\w-]+\.)+[\w-]+
which is supposed to be simple, and weed out the most obvious problems, without being too picky.
Anywho, given the input bob.smith@someplace.co.uk – the RegularExpressionValidator fails, but the CustomValidator passes. Other scenarios work as expected.. both pass blah@blah.com, but would fail blah.com. There may be other issues but this is one I noticed.
I guess the code in the CustomValidator isn’t the same as what the RegularExpressionValidator would produce ‘behind the scenes’ – but what exactly is the difference and why do I see what I see?
many thanks!
contains a dot before the
@. That dot isn’t matched by[\w-]+.The RegularExpressionValidator checks the entire string against the regex and, correctly, fails.
The CustomValidator (using
Regex.IsMatch()) checks if a substring matches (and succeeds withsmith@someplace.co.uk, again correctly).To make sure that both behave the same, surround your regex with anchors: