?

Log in

No account? Create an account

ms_dot_net

Client-side validation

« previous entry | next entry »
Feb. 27th, 2009 | 05:13 pm
location: Work
mood: frustratedfrustrated
music: Linkin Park — Numb
posted by: owenblacker in ms_dot_net

I’m having some “fun” with client-side validation on a roll-my-own BaseValidator subclass.

My validator is to validate email format and includes the following code (with XMLdoc removed for brevity):

using System;
using System.Text.RegularExpressions;

namespace IrisDigital.WebControls.Validation
{
   [System.Web.UI.ToolboxData("<{0}:EmailValidator runat='server'/>")]
   public class EmailValidator : BaseValidator
   {
      protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
      {
         base.AddAttributesToRender(writer);

         if (base.RenderUplevel)
         {
            writer.AddAttribute("evaluationfunction", "CheckEmailFormat");
            writer.AddAttribute("controltovalidate", this.GetControlRenderID(this.ControlToValidate));
         }
      }

      protected override bool EvaluateIsValid()
      {
         // ...
      }

      protected override void OnPreRender(EventArgs e)
      {
         base.OnPreRender(e);

         if (base.RenderUplevel)
         {
            if (!Page.ClientScript.IsClientScriptBlockRegistered("IrisDigital.WebControls.Validation.EmailValidator"))
            {
               Page.ClientScript.RegisterClientScriptInclude("IrisDigital.WebControls.Validation.EmailValidator",
                  Page.ClientScript.GetWebResourceUrl(this.GetType(), "IrisDigital.WebControls.Validation.EmailValidator.js"));
            }
            this.ClientValidationFunction = "CheckEmailFormat";
         }
      }

Most of the rationale for doing things this way is from an article on CodeProject: RequiredIfValidator — Extending from the BaseValidator class, which does it precisely this way.

The validator works wonderfully. But the client-side validation never fires.

The WebResource correctly gets added to the page as a script include (for which I need to have the following line in my AssemblyInfo.cs, in case you were wondering):

[assembly: System.Web.UI.WebResource("IrisDigital.WebControls.Validation.EmailValidator.js", "text/javascript")]

, but it just never gets called.

Anyone got any ideas why?

Cross-posted to csharp and owenblacker.

Link | Leave a comment | Share

Comments {0}