简体   繁体   中英

ASP.NET CustomValidator client side

I can't get this CustomValidator working.

In the <head>:

<script language="javascript" type="text/javascript">
  function ValidateFile(sender, args){
      alert("Hi");

      args.IsValid = document.getElementById("fuFile").value != "" || 
                     document.getElementById("c101_c7").value != "";
  }
</script>

In the body:

<asp:FileUpload ID="fuFile" runat="server" size="70"/>
<asp:TextBox ID="c101_c7" class="textbox" runat="server"/>
<asp:CustomValidator ID="vldFile" runat="server" ClientValidationFunction="ValidateFile"
    ErrorMessage="You must either upload a file or provide a URL of a file."></asp:CustomValidator>

What should be in the args.IsValid if either the FileUpload or TextBox has to be filled in?

我发现实际让后面的代码告诉你的JavaScript代码控件的客户端ID是有用的,因为它可能与你想的不同(根据ASP .NET决定做的):

document.getElementById('<%=fuFile.ClientID %>');
<script type="text/javascript">
//<![CDATA[
    function validateField(sender, args) {        
        var regExp = /(^[a-zA-Z]{2,50})$/;
        var val = document.getElementById(sender.controltovalidate).value;
        args.IsValid = regExp.test(val);
}
//]]>
</script>

are you just using a normal button to trigger the validation?

Are you implementing validationgroups anywhere else in this code?

You need to set the ControlToValidate Property on the custom validator. Currently the validator is not hooked up to any control.

Also, I'm sure you just didn't post this part of the markup, but you need to have a control that does a postback and causes validation as part of its postback. A button will work for this.

这有效

document.getElementById("ctl00_ContentPlaceHolder1_fuFile").value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM