简体   繁体   中英

Validation for form on Button Click

I have a form where I have many textboxes which are getting validated through Required field validator , But there is a captcha control also which is not getting validated. However I can validate it through javascript.

Here alert gets displayed when I click on 'OK' of alert message box. But I want this message to be displayed on button click simultaneously with other validations.

Please help. Thanks in Advance.

I think you want to display the "Please enter captcha text." as part of the ValidationSummary control.

To do this, you just need to let the server-side know the captcha text. Do this:

  1. Add another textbox, but keep it hidden with display:none :

    <asp:textbox id="txtCaptcha" runat="server" style="display:none;">

  2. Modify your Javascript to copy the captcha value into the txtCaptcha textbox:

     <script type="text/javascript"> $(document).ready(function () { $('#' +'<%=btnSend.ClientID %>').on('click', function (e) { var captcha = $("input[name$=CaptchaControl1]").val(); $('#' + '<%=txtCaptcha.ClientID %>').val(captcha); }) }); </script> 
  3. Create a RequiredValidator for txtCaptcha textbox:

    <asp:RequiredFieldValidator ID="RequiredFieldValidatorForCaptcha" runat="server" ErrorMessage="Please enter captcha text." ControlToValidate="txtCaptcha" Display="None" ValidationGroup="VG"></asp:RequiredFieldValidator>

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