Search code examples
asp.netvalidationclient-side-validation

ASP.NET form validator display dynamic, adjust CSS of input?


I've got my dynamic validator working, (its creating a span on invalidation client-side), is there a way to control the styling of the invalid input on client-side val? I want to give it a redish background. Looking for a light-weight simplistic solution but open to all options.

Thanks!


Solution

  • To Exactly quote a previous answer of mine:

    This article might help you:

    http://msdn.microsoft.com/en-us/library/aa479045.aspx

    Particularly this section (look for "Client Side Validation" then under there, "Special Effects"):

    <asp:Label id=lblZip runat=server 
       Text="Zip Code:"/> 
    <asp:TextBox id=txtZip runat=server 
       OnChange="txtZipOnChange();" /></asp:TextBox><br>
    <asp:RegularExpressionValidator id=valZip runat=server
       ControlToValidate=txtZip
       ErrorMessage="Invalid Zip Code" 
       ValidationExpression="[0-9]{5}" /><br>
    
    <script language=javascript>
    function txtZipOnChange() {
       // Do nothing if client validation is not active
       if (typeof(Page_Validators) == "undefined")  return;
       // Change the color of the label
       txtZip.style.color = valZip.isvalid ? "Black" : "Red";
    }
    </script>
    

    There is still some wiring up that needs to be done, which you may be able to tidy up with some jQuery or the like