You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Keshav a176ec103b Update 'README.md' 4 months ago
README.md Update 'README.md' 4 months ago

README.md

Custom form validation & error handling in webflow

Webflow's native form validation is helpful, but sometimes you need finer control and custom error handling. Here's how to achieve that with short and clean code:

1. Basic Validation with JavaScript:

  • Assign IDs or unique classes to your input fields.
  • Write a JavaScript function that checks each field value against your validation rules (e.g., required, email format, minimum length).
  • Use DOM manipulation to display error messages next to invalid fields.

Example:

JavaScript
function validateForm() {
  const nameInput = document.getElementById('name');
  const emailInput = document.getElementById('email');
  const messageInput = document.getElementById('message');

  let isValid = true;

  if (!nameInput.value) {
    showError(nameInput, 'Name is required.');
    isValid = false;
  } else {
    hideError(nameInput);
  }

  if (!emailInput.value || !/\S+@\S+\.\S+/.test(emailInput.value)) {
    showError(emailInput, 'Please enter a valid email address.');
    isValid = false;
  } else {
    hideError(emailInput);
  }

  if (!messageInput.value || messageInput.value.length < 10) {
    showError(messageInput, 'Message must be at least 10 characters long.');
    isValid = false;
  } else {
    hideError(messageInput);
  }

  return isValid;
}

function showError(element, message) {
  const errorElement = document.getElementById(element.id + '-error');
  errorElement.textContent = message;
  errorElement.classList.add('active');
}

function hideError(element) {
  const errorElement = document.getElementById(element.id + '-error');
  errorElement.textContent = '';
  errorElement.classList.remove('active');
}

document.getElementById('contact-form').addEventListener('submit', function(event) {
  event.preventDefault();
  if (!validateForm()) return;

  // Submit form using Webflow API or other method
});

2. Advanced Validation with Libraries:

  • Use JavaScript libraries like Formik or Formspree for a more robust validation experience.
  • Leverage features like dynamic error messages, field-level validation, and real-time feedback.

3. Webflow Interactions for Error Styling:

  • Create Interactions in Webflow to trigger custom animations or styling changes on invalid fields.
  • This adds visual flair and improves user experience for error handling.

Need Help?

Need a High Converting SaaS Landing Page?