You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为现有表单验证脚本添加ReCaptcha2勾选验证

Fix reCAPTCHA 2 Validation in Your Form

Got it, let's resolve that issue where users can submit your form without completing the reCAPTCHA 2 challenge. The goal is to add reCAPTCHA validation logic to your existing script while keeping the error styling consistent with your current setup.

Step 1: Understand the reCAPTCHA 2 Hidden Field

reCAPTCHA 2 automatically creates a hidden input with the ID g-recaptcha-response when the user completes the challenge. If this field is empty, it means the user hasn't checked the "I'm not a robot" box.

Step 2: Integrate Validation into Your Existing Script

Locate your form's validation function (the one that runs before submission) and add the reCAPTCHA check. Here's how to do it, using a common validation structure as an example:

function validateForm() {
  let formIsValid = true;

  // --- Your existing field validation logic (keep this intact) ---
  const nameField = document.getElementById('user-name');
  if (!nameField.value.trim()) {
    showValidationError(nameField, 'Please enter your name');
    formIsValid = false;
  } else {
    clearValidationError(nameField);
  }

  // Repeat similar checks for email, message, etc.
  // --- End of existing validation ---

  // --- Add reCAPTCHA validation here ---
  const recaptchaResponse = document.getElementById('g-recaptcha-response').value;
  const recaptchaContainer = document.querySelector('.g-recaptcha');

  if (!recaptchaResponse) {
    // Use your existing error display function to keep styling consistent
    showValidationError(recaptchaContainer, 'Please complete the reCAPTCHA verification');
    formIsValid = false;
  } else {
    // Clear any previous reCAPTCHA error
    clearValidationError(recaptchaContainer);
  }

  return formIsValid; // Return false to block submission if invalid
}

// Assume these are your existing error handling functions (adjust to match your code)
function showValidationError(element, errorText) {
  // Create or update error element next to the input/container
  let errorElement = element.nextElementSibling;
  if (!errorElement || !errorElement.classList.contains('form-error')) {
    errorElement = document.createElement('div');
    errorElement.classList.add('form-error'); // Match your existing error class
    element.parentNode.appendChild(errorElement);
  }
  errorElement.textContent = errorText;
  element.classList.add('invalid-input'); // Match your existing invalid field class
}

function clearValidationError(element) {
  const errorElement = element.nextElementSibling;
  if (errorElement && errorElement.classList.contains('form-error')) {
    errorElement.remove();
  }
  element.classList.remove('invalid-input');
}

Step 3: Ensure the Form Uses the Validation

Make sure your form's onsubmit event calls your validation function:

<form onsubmit="return validateForm()">
  <!-- Your form fields + reCAPTCHA container -->
  <div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY"></div>
  <button type="submit">Submit</button>
</form>

Key Notes:

  • Replace YOUR_SITE_KEY with your actual reCAPTCHA site key.
  • Adjust the selector for recaptchaContainer if your reCAPTCHA has a different class or ID.
  • Match the class names (form-error, invalid-input) to the ones used in your existing validation styling to ensure consistency.

This check will block form submission until the user completes the reCAPTCHA, and the error message will look identical to your other field validation errors.

内容的提问来源于stack exchange,提问作者user2764806

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:31:18