ASP.NET - CausesValidation to true before javascript function get fired onClien

Asked By Daniel on 26-Feb-13 02:08 PM
Hi,I have textboxes that causesValidtion once linkbutton get fired if empty,but I have called a javascript function OnclientClick and the CausesValidation is working after the linkButton was clicked...
let me be specifik by giving an exemple

Javascript code:
          function generateRandom() {
                $('#txtRefcode').val(random());
                alert('Reference Code: ' + $('#txtRefcode').val());
                return true;
            }
            function random() {
                return Math.floor(Math.random() * 10000000);
            }
Html code:

<asp:TextBox ID="txtsname" runat="server"  CssClass="TextBox-size"></asp:TextBox></td>
<asp:RequiredFieldValidator ID="txtsnameRfValidator" runat="server" ErrorMessage="*" Text="Second Name is required" ControlToValidate="txtsname" Display="Dynamic"></asp:RequiredFieldValidator>

<asp:LinkButton id="TigoCash" Cssclass="borderit" runat="server" OnClick="linkbuttonTigoCash_click" CausesValidation="true" OnClientClick="return generateRandom();" >

What I want is to causesValidation to true if textbox is empty before the generateRandom() function get called.
Danasegarane Arunachalam replied to Daniel on 27-Feb-13 03:57 AM
How about this one ?


function generateRandom() {
          
        //Check the value of textbox
           Var lastname= $("#txtsname");
         //if the textbox is empty exit
        //else process the  validation
        if (inp.val().length == 0) {
             return false // textbox empty
          }
        $('#txtRefcode').val(random());
        alert('Reference Code: ' + $('#txtRefcode').val());
        return true;
      }
      function random() {
        return Math.floor(Math.random() * 10000000);
      }