I’m not a big fan of ASP.NET WebForms, and one of my pain points is form validation. It seems a bit heavy-handed and clumsy compared to, say, Dynamic Data or MVC. Generally, it’s adequate.
To validate that a textbox is required is easy enough, just use a RequiredFieldValidator. To validate the text is in some specific format seems easy enough too, just use RegularExpressionValidator. But the “gotcha” that may not be immediately apparent is that using the RegularExpressionValidator as is requires you to violate DRY, since you must specify the ValidationExpression each time you use it. With this, you get all the standard pitfalls of DRY, including the possibility of a phone number formatting on Form1.aspx being totally different than the phone number formatting on Form2.aspx. So, wat do?
Here’s what I did: create a more specific implementation of RegularExpressionValidator called PhoneValidator
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Whatever
{
[ToolboxData("<{0}:PhoneValidator runat=\"server\" />")]
public class PhoneValidator : RegularExpressionValidator
{
public PhoneValidator()
{
ValidationExpression = @"^([\(]{1}[0-9]{3}[\)]{1 ... $";
}
}
}
And now you can use PhoneValidator just as you would RegularExpressionValidator, except the ValidationExpression only exists in one place for consistency and ease of change. Don’t forget to register the assembly that the above class is in on your page:
<%@ Register assembly="WhateverAssembly" namespace="Whatever" tagprefix="MyValidator" %>
(Or you could register it in your web.config for use in the whole app). Once it’s registered, just do this (it should appear in intellisense as well):
<MyValidator:PhoneValidator ControlToValidate="txt_phone_number" runat="server" />
Register assembly + PhoneValidator control + Phone Validator class is a hell of a lot longer than the regex validator. Is abstraction what you’re going for, Picasso?
Who cares about longer? What happens when you validate phone numbers in 100 places in your web app, and suddenly you have to accommodate foreign phone numbers, or phone number without area codes, etc? Now you have to change your regex expression in 100 places. And the assembly registration can be in one place too (web.config).
I believe Control + H would solve that problem.
You are just rejecting DRY then? Those 100 instances could individually change and then your find/replace doesn’t work, and now you’re stuck with a broken app and changing 100 instances by hand. In more technical terms, you are preferring connascence of algorithm over connascence of name, which leads to a tightly coupled system with poor maintainability and poor quality. Fail.
I understand your goal — I just see the regex value as a synonym for "phone".
…sLogin to VoteDon’t repeat your validation (2/25/2010)Thursday, February 25, 2010 from mgrovesIâ??m not a big fan of ASP.NET WebForms, and one of my pa…
Also, if you did need to change the phone inputs across the entire application (to accommodate international numbers for instance) you’d have change the Columns and/or MaxLength parameters on all of the text controls anyway.
#7 is a good point. If that is the case, I would consider putting the whole textbox and validator package together into a user control.