0

我有一个自定义验证器,并希望它验证客户端而不是服务器端。这是我的功能:

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>

任何人都可以在这方面帮助我。提前致谢。

4

1 回答 1

2

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!

于 2011-12-15T10:18:41.623 回答