contact_our_team.js

A client-side script that enhances the Contact Our Team form with real-time input sanitization, validation rules, and Google reCAPTCHA v3 integration. It prevents malicious or malformed input and ensures only valid submissions reach the server.

📋 Dependencies

  • jQuery (for DOM readiness and event handling)
  • jQuery Validation Plugin (.validate())
  • Google reCAPTCHA v3 (for spam protection)

✂️ Paste Event Sanitization

Attaches to all <input> and <textarea> fields inside the form.
Prevents pasting of:

  • HTML tags (<[a-z][\s\S]*> )
  • JSON-style objects ({.*})
  • Common script/style patterns (script, style, <, >)
$("#contact_our_team_form input, #contact_our_team_form textarea")
  .on("paste", function (e) {
    let clipboardData = (e.originalEvent || e)
      .clipboardData.getData("text");

    // Block HTML/CSS/JS snippets
    if (
      /<[a-z][\s\S]*>/i.test(clipboardData) ||
      /{.*}/.test(clipboardData) ||
      /(script|style|<|>)/i.test(clipboardData)
    ) {
      e.preventDefault();
      alert("HTML, CSS or JavaScript is not allowed!");
      return false;
    }
});

🚫 Keypress Event Sanitization

Disallows typing of the following characters:

  • <, >
  • {, }
$("#contact_our_team_form input, #contact_our_team_form textarea")
  .on("keypress", function (e) {
    let key = String.fromCharCode(e.which);

    if (/[<>]/.test(key) || /[{}/]/.test(key)) {
      e.preventDefault();
      alert("Special characters < > { } are not allowed!");
    }
});

✅ Form Validation Rules

Initializes jQuery Validate on submission:

  • Required Fields:

    • fName (First Name)
    • lName (Last Name)
    • company_name
    • company_email (must be a valid email)
    • company_phone
    • detail_description
  • Custom Messages for user feedback

$("#contact_our_team_form").validate({
  errorClass: "jquery_validate_error",
  wrapper: "span",
  rules: {
    fName:            { required: true },
    lName:            { required: true },
    company_name:     { required: true },
    company_email:    { required: true, email: true },
    company_phone:    { required: true },
    detail_description:{ required: true }
  },
  messages: {
    fName: { required: "First Name is Required" },
    lName: { required: "Last Name is Required" },
    company_name: { required: "Company is Required" },
    company_email: {
      required: "Email is Required",
      email:    "Enter Valid Email Address"
    },
    company_phone: { required: "Phone is Required" },
    detail_description:{ required: "Detail Description is Required" }
  },
  submitHandler: handleRecaptcha
});

🔒 Google reCAPTCHA v3 Integration

On successful client validation, invokes reCAPTCHA before final submission:

function handleRecaptcha(form) {
  grecaptcha.ready(function() {
    grecaptcha.execute(
      '6LexaqwrAAAAAKs-aDNL0G-vBf4mcrVd6I1naxO7',
      { action: 'submit_form' }
    ).then(function(token) {
      console.log(token);
      $('#g-recaptcha-response').val(token);
      form.submit();
    });
  });
}
  • Site Key: 6LexaqwrAAAAAKs-aDNL0G-vBf4mcrVd6I1naxO7
  • Action: submit_form
  • Hidden Field Required:
    <input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response" />
    

🎨 Initialization & Usage

  1. Include jQuery, jQuery Validate, and the reCAPTCHA v3 script in your page head.
  2. Place this JS file after those libraries.
  3. Ensure your form has the ID contact_our_team_form and contains matching input names.
  4. Add the hidden reCAPTCHA input inside the form.
<form id="contact_our_team_form" method="post" action="/submit">
  <input type="text" name="fName" />
  <!-- ... other fields ... -->
  <input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response" />
  <button type="submit">Send</button>
</form>
<script src="https://www.google.com/recaptcha/api.js?render=6LexaqwrAAAAAKs-aDNL0G-vBf4mcrVd6I1naxO7"></script>
<script src="js/contact_our_team.js"></script>

🌐 Workflow Diagram

flowchart TD
  A[Document Ready] --> B[Bind Paste Handler]
  A --> C[Bind Keypress Handler]
  A --> D[Initialize Validation]
  
  B --> E[On Paste]
  E --> F{Invalid Content?}
  F -- Yes --> G[Prevent & Alert]
  F -- No  --> H[Allow Paste]
  
  C --> I[On Keypress]
  I --> J{Forbidden Char?}
  J -- Yes --> K[Prevent & Alert]
  J -- No  --> L[Allow Key]
  
  D --> M[On Form Submit]
  M --> N[Client-Side Validation]
  N -- Valid --> O[Invoke reCAPTCHA]
  O --> P[Receive Token]
  P --> Q[Set Hidden Input & Submit]
  N -- Invalid --> R[Show Errors]

This script provides a robust front-end layer for input hygiene and spam prevention, improving both security and user experience.