Client-side validation

    Date: 02/27/09 (C Sharp)    Keywords: html, xml, java, web

    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 '[info]'ms_dot_net and '[info]'owenblacker.

    Source: http://community.livejournal.com/csharp/102003.html

« BackgroundWorker || Vista/2k3 C# Packet Capturing »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home