开发者

customvalidator client side validation

开发者 https://www.devze.com 2023-03-14 05:55 出处:网络
I have a custom validator and want it to validate client side on not server side. Here is my开发者_C百科 function :

I have a custom validator and want it to validate client side on not server side. Here is my开发者_C百科 function :

function checkCampDockTemplate(source, args)
    {
        var campDockTemplate = document.getElementById('<%= drpCampDockTemplate.ClientID %>');

        if (campDockTemplate.selectedIndex == 0)
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }



<asp:CustomValidator ID="campaignDocketTemplateCustomValidator" runat="server" ErrorMessage="Please select a Campaign Docket Template"
            ControlToValidate="drpCampDockTemplate" OnServerValidate="campaignDocketTemplateCustomValidator_ServerValidate"
            ClientValidationFunction="javascript:checkCampDockTemplate()"></asp:CustomValidator>

Can any one please assist me on this. Thanks in advance.


You've got the format of the function mostly correct, but you need to reference the client-side function by name only in the ClientValidationFunction attribute, e.g.

<asp:CustomValidator ID="campaignDocketTemplateCustomValidator" 
runat="server" 
ErrorMessage="Please select a Campaign Docket Template"
ControlToValidate="drpCampDockTemplate"     
OnServerValidate="campaignDocketTemplateCustomValidator_ServerValidate"
ClientValidationFunction="checkCampDockTemplate"/>

If you also want to remove the server-side validation completely, you can remove the OnServerValidate attribute from the CustomValidator.

Generally speaking though, it's usually wise to validate your input on the server as well as on the client side, in case people have javascript turned off!

0

精彩评论

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

关注公众号