Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Programming (https://www.warriorforum.com/programming/)
-   -   Recaptcha 2.0 validation problem (https://www.warriorforum.com/programming/1069438-recaptcha-2-0-validation-problem.html)

Haship 27th April 2015 11:14 AM

Recaptcha 2.0 validation problem
 
I am having a problem with recaptcha 2.0 validation. I already added captcha into my form, now I need to make it validate captcha before sending message via form.

Here is a whole validation code:

Code:

$(document).ready(function(){

//global vars

    var enquiryfrm = $("#agt_mail_agent");

    var yourname = $("#agt_mail_name");

    var yournameInfo = $("#span_agt_mail_name");

    var youremail = $("#agt_mail_email");

    var youremailInfo = $("#span_agt_mail_email");

    var frnd_comments = $("#agt_mail_msg");

    var frnd_commentsInfo = $("#span_agt_mail_msg");

   

    //On blur

    yourname.blur(validate_yourname);

    youremail.blur(validate_youremail);

    frnd_comments.blur(validate_frnd_comments_author);

    //On key press

    yourname.keyup(validate_yourname);

    youremail.keyup(validate_youremail);

    frnd_comments.keyup(validate_frnd_comments_author);

   

    //On Submitting

    enquiryfrm.submit(function(){

        if(validate_yourname() & validate_youremail() & validate_frnd_comments_author())

        {
            //hideform();
            return true
        }
        else
        {
            return false;
        }
    });



    //validation functions

    function validate_yourname()

    {

        if($("#agt_mail_name").val() == '')

        {

            yourname.addClass("error");

            yournameInfo.text("Please Enter Your Name");

            yournameInfo.addClass("message_error2");

            return false;

        }

        else{

            yourname.removeClass("error");

            yournameInfo.text("");

            yournameInfo.removeClass("message_error2");

            return true;

        }

    }

    function validate_youremail()

    {

        var isvalidemailflag = 0;

        if($("#agt_mail_email").val() == '')

        {

            isvalidemailflag = 1;

        }else

        if($("#agt_mail_email").val() != '')

        {

            var a = $("#agt_mail_email").val();

            var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;

            //if it's valid email

            if(filter.test(a)){

                isvalidemailflag = 0;

            }else{

                isvalidemailflag = 1;   

            }

        }

        if(isvalidemailflag)

        {

            youremail.addClass("error");

            youremailInfo.text("Please Enter valid Email Address");

            youremailInfo.addClass("message_error2");

            return false;

        }else

        {

            youremail.removeClass("error");

            youremailInfo.text("");

            youremailInfo.removeClass("message_error");

            return true;

        }

       

    }

   

    function validate_frnd_comments_author()
    {               
        if($("#agt_mail_msg").val() == '')
        {
            frnd_comments.addClass("error");
            frnd_commentsInfo.text("Please Enter Comments");
            frnd_commentsInfo.addClass("message_error2");
            return false;
        }else{
            frnd_comments.removeClass("error");
            frnd_commentsInfo.text("");
            frnd_commentsInfo.removeClass("message_error2");
            return true;
        }

    }   
function reset_email_agent_form()
{
    document.getElementById('agt_mail_name').value = '';
    document.getElementById('agt_mail_email').value = '';
    document.getElementById('agt_mail_phone').value = '';
    document.getElementById('agt_mail_msg').value = '';   
}
});

I don't know where exactly I should put validation code for recaptcha. I need help.

emptee 27th April 2015 02:49 PM

Re: Recaptcha 2.0 validation problem
 
Hey mate,

You can't validate the captcha from JS, as you don't have the secret key at that point.. It's not really useful to do anyway IMO :)

You'll need some code like this on your forms action page:

$secret = 'your recaptcha secret';
$captcha_url = "https://www.google.com/recaptcha/api/siteverify?secret=${secret}&response=".$_POST['g-recaptcha-response'].'&remoteip='.$_SERVER['REMOTE_ADDR'];
if ($data = file_get_contents($captcha_url)) {
$obj = json_decode($data);
if (! $obj->success) {
//redirect back to form with error message, or something...
} else {
//process form and do whatever you want - the captcha was good
}
}

Cheers,
Michael

Haship 28th April 2015 05:32 AM

Re: Recaptcha 2.0 validation problem
 
Thanks Michael, I'll try your given code.


All times are GMT -6. The time now is 01:12 PM.