Friday, July 16, 2010

ASP.Net custom validator with javascript

To validate controls using javascript, just so you can use it for XUI or jQuery or whatever Web 2.0 library you use, you can simply declare a ASP.Net Custom Validator as such and have the ClientValidationFunction to be your javascript validator function;

<asp:CustomValidator ID="someVal" runat="server" ClientValidationFunction="jsValidator"></asp:CustomValidator>

Then, in the section of where your javascript code resides, can be in a file or at the top of the same page, declare a function as such.

function jsValidator(args, sender)
    //Insert validation here.
    //To set the error message on the ASP.Net validator control, just use the
    //sender object as such;
    //sender.errormessage = "Some error message";
    //Also, remember to set the isValid boolean flag, "args.IsValid"

No comments:

Post a Comment