开发者

Display Field Validators Manually (ASP.NET/C#)

Imagine the scenario of a contact form, whereby data is to be collected and further transmitted via email to the appropriate address. One of the important fields asks for an email address, pretty vital to instrumenting the call-back.

So say I check for the entry of something, anything, in the mail address field using a RequiredFieldValidator; a further check in code will determine if the mail address supplied is usable, or not.

Given that both of these checks are essentially validation, I would simply like to reuse the RequiredFieldValidator from my code-behind; is this possible?

For example, something as simple as:

MyValidator.Show();
return;

What I have done in the past is add a Label to the form as well, and just use a similar tactic, only making the control visible as needs be and returning. Ideally I'd like to get rid of this altogether and simply use one control to output my error message.

For an idea of current code, below is a skeletal listing, which, even as such, is the concept in its entirety:

Within the page:

<asp:TextBox runat="server" ID="MailAddressTextBox" />

<asp:RequiredFieldValidator runat="server" ID="MailAddressValidator" ErrorMessage="Enter a valid mail address" Co开发者_运维知识库ntrolToValidate="MailAddressTextBox" />

<asp:Label runat="server" ID="MailAddressError" Text="Enter a valid mail address" Visible="false" />

And within the code-behind:

MailAddress enquirerMail;
try
{
    enquirerMail = new MailAddress(MailAddressTextBox.Text);
}
catch
{
    MailAddressError.Visible = true;
    return;
}

The pitfall with the approach of hiding/showing error labels, other than the obvious of extra controls, is the maintenance of their visibility, and also a label for each error, or lists of messages and in-code alterations to the display.

Also, multiple validators is something I want to avoid, at least in this particular instance; a RegularExpressionValidator won't cut it here, as we all know just selecting the 'best' regex for parsing mail addresses can be a feat in itself, or maybe it will contain more characters than the actual page content + markup.

Another reason for my aversion to regex, should you need one, is that, ultimately, I have to implement the above try/catch validation regardless, as even a 'validated' address may cause failure in the MailAddress constructor.

So, just to reiterate: Can I activate/reuse my validators from code-behind?


I really think that if you're validating two things (the fact that the field is not empty AND the fact that it's a valid address), then you should have two validators.

However, to answer your question, you could do:

MailAddressValidator.ErrorMessage = "Invalid email address";
MailAddressValidator.IsValid = false;


If you really want to avoid using multiple validators (I can understand why, generating "in-line" error messages will not be as neat with two or more validation controls), I suggest using a single custom validator.

This means you can write your own validation code (including a case where the user has not provided ANYTHING and another case where they have provided an invalid email address) and have precise control over the error message rendered.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜