jeudi 18 juin 2015

The Google reCAPTCHA doesn't work with ASPX

I try to make a recaptcha system on my ASPX form and i can use asp:CustomValidator for all the fields and they show me the errors messages (required or incorrect) but it's not the case for the asp:CustomValidator of recaptcha:

Here is a part of my form page:

<div style="display: inline-table; width: 100%;padding-left:3%;">
      <asp:Label ID="Label1" runat="server" Text="Number of phone " CssClass="text"></asp:Label>
      <b class="te_red">* </b>                             
      <asp:TextBox ID="Tb_Li" runat="server" CssClass="log_tb" MaxLength="10"></asp:TextBox>
      <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Can you make a number phone." Display="None" ControlToValidate="Tb_Li" OnServerValidate="cv_validate_nPhone" ValidateEmptyText="true" ValidationGroup="mugroupValid"></asp:CustomValidator>
</div>
<script type="text/javascript" src="http://ift.tt/1yW6XBL"
async defer></script>
<script type="text/javascript">
    var onloadCallback = function () {
         grecaptcha.render('dvCaptcha', {
         'sitekey': '<%=ReCaptcha_Key %>',
         'callback': function (response) {
         $.ajax({
         type: "POST",
         url: "http://ift.tt/1Im50Sw",
         data: "{response: '" + response + "'}",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success: function (r) {
         var captchaResponse = jQuery.parseJSON(r.d);
         if (captchaResponse.success) {
               $("[id*=txtCaptcha]").val(captchaResponse.success);
               $("[id*=rfvCaptcha]").hide();
         } else {
               $("[id*=txtCaptcha]").val("");
               $("[id*=rfvCaptcha]").show();
               var error = captchaResponse["error-codes"][0];
               $("[id*=rfvCaptcha]").html("RECaptcha error. " + error);
         }
        }
       });
      }
     });
    };
  </script>
  <div id="dvCaptcha">
  </div>
  <asp:TextBox ID="txtCaptcha" runat="server" Style="display: none" />
<asp:CustomValidator ID="rfvCaptcha" runat="server" ErrorMessage="Veuillez cocher la case que vous n'êtes pas un robo." 
                                    Display="None" ControlToValidate="Tb_Ligne"  ValidateEmptyText="true" OnServerValidate="cv_validate_captcha" ValidationGroup="mugroupValid"></asp:CustomValidator>
<asp:LinkButton ID="btn_validate" runat="server" Text="Validate"   OnClick="btn_validate_Click" ValidationGroup="mugroupValid" CausesValidation="true" />

This is a part of the code of my ContacForm.aspx.cs:

public partial class ContacForm: System.Web.UI.Page
{
protected static string ReCaptcha_Key = "<RECaptcha Site Key>";
protected static string ReCaptcha_Secret = "<RECaptcha Secret Key>";

[WebMethod]
public static string VerifyCaptcha(string response)
 {
    string url = "http://ift.tt/1vh5G4o" + ReCaptcha_Secret + "&response=" + response;
    return (new WebClient()).DownloadString(url);
 }


protected void cv_validate_captcha(object sender, ServerValidateEventArgs e)
    {

        e.IsValid = txtCaptcha.Text.ToString().Length > 0;

    }

protected void cv_validate_nPhone(object sender, ServerValidateEventArgs e)
    {
        Regex myRegex = new Regex(@"[0-9]{10}$");
        e.IsValid = myRegex.IsMatch(e.Value.ToString());
    }
}

When I completed the form without filling fields that does not pass by telling me that I have to fill everything and when I make them without checking the box of the captcha the form passes while as a rule that has to not pass

Aucun commentaire:

Enregistrer un commentaire