Link to home
Start Free TrialLog in
Avatar of nikaotech
nikaotech

asked on

reCaptcha with pop up on validation error

Hi Experts,

We've got several forms throughout our site and the amount of spam/bot generated empty forms is getting annoying...thus I've been reading and working with reCaptcha.  Everything's great except for the out of the box way the error handling is displayed for the end user.  All of our forms use some simple javascript to pop up an error if any of the required fields are not filled out correctly.  I'd like to do the same with reCaptcha.  Below is the form validation we use along with the OOTB reCaptcha...hoping someone can show me how to integrate the two so when reCaptcha fails it doesn't require loading a new page...

(Our Current Form Validation)

<script type="text/javascript">
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    }
      if (document.getElementById('orientationDate').selectedIndex < 1) {
      errors += '- Orientation Date is required.\n';
    }
    if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
  }
}
</script>

Open in new window


(reCaptcha Validation)

<?php  require_once('recaptchalib.php');  
$privatekey = "";  
$resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);  
if (!$resp->is_valid) {    
  // What happens when the CAPTCHA was entered incorrectly    
  die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .         
       "(reCAPTCHA said: " . $resp->error . ")");  
} else {    
  // Your code here to handle a successful verification  
}  
?> 

Open in new window



Thanks for any input!
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@nikaotech: You left this question open for months, without comment, and now closed it with a bad grade.  Perhaps you did that jist to "make it go away" so you could ask a new question?  Here are the grading guidelines.
https://www.experts-exchange.com/help/viewHelpPage.jsp?helpPageID=26

You asked for something that does not exist and cannot exist, given your requirement for reCaptcha.  I will ask a moderator to reopen the question so you can explain the bad grade.