Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 27th Apr 2015, 11:14 AM   #1
Active Warrior
 
Join Date: 2013
Posts: 70
Thanks: 20
Thanked 2 Times in 2 Posts
Arrow
Recaptcha 2.0 validation problem
Share on: 
fb share twitter share gplus share more share

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.
Haship is offline  
Unread 27th Apr 2015, 02:49 PM   #2
HyperActive Warrior
War Room Member
 
emptee's Avatar
 
Join Date: 2014
Location: New Zealand
Posts: 210
Thanks: 25
Thanked 81 Times in 71 Posts
Default
Re: Recaptcha 2.0 validation problem
Share on: 
fb share twitter share gplus share more share

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

emptee is offline  
The Following User Says Thank You to emptee For This Useful Post:
Unread 28th Apr 2015, 05:32 AM   #3
Active Warrior
 
Join Date: 2013
Posts: 70
Thanks: 20
Thanked 2 Times in 2 Posts
Default
Re: Recaptcha 2.0 validation problem
Share on: 
fb share twitter share gplus share more share

Thanks Michael, I'll try your given code.
Haship is offline  
Closed Thread


Bookmarks

Tags
problem, recaptcha, validation


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




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