Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 349
Aximili Avatar asked Dec 28 '25 12:12

Aximili


2 Answers

I find it helpful to actually let the code behind tell your JavaScript code what the client-side ID of the control is, since it's possible it's different than what you would think (based on what ASP .NET decides to do):

document.getElementById('<%=fuFile.ClientID %>');
like image 116
Brandon Montgomery Avatar answered Dec 31 '25 02:12

Brandon Montgomery


<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>
like image 21
Hakan Avatar answered Dec 31 '25 02:12

Hakan