How to add conditional validation to Webflow forms using Memberstack data to prevent submission when user input exceeds available credits? Answered
Hey! Is there any way to have conditional logic in a Webflow form so I can prevent form submission if a user enters a number in a field that is greater than the number in another field that is populated from Memberstack?
Example: Request to book a hotel form
- User enters a request in the form to stay "3" nights
- I use data-ms-member to populate a hidden field with their booking credits which = "2" credits
- I want to prevent form submission and return an error because # nights > # credits
I have been trying to do this for hours and can't figure it out. Really appreciate any help or a point in the right direction!
Comments
7 comments
Hi Chase,
Yes, you can achieve with some Javascript. Here's a snippet that would help. Make sure to replace "your-form-id", "nights-field", "credits-field" with the actual IDs or class names of your form and fields.
(NEWER SCRIPT IN COMMENT BELOW)<script>document.addEventListener("DOMContentLoaded", function() {const form = document.querySelector("#your-form-id"); // Replace with your actual form IDconst nightsField = document.querySelector("#nights-field"); // Replace with your nights field IDconst creditsField = document.querySelector("[data-ms-member='booking-credits']"); // Use the data-ms-member attributeform.addEventListener("submit", function(event) {const nights = parseInt(nightsField.value);const credits = parseInt(creditsField.value);if (nights > credits) {event.preventDefault();alert( "Number of nights exceeds available credits.");}});});</script>Thank you so much! I'm still getting errors but I think another script I'm running is interfering so i'm going to build a separate page to use this in. Really appreciate your help.
You're welcome 🙂
This code works, except that after it shows the error and I click "ok" in the error modal, the form submits. any idea how to prevent that? Thanks again for your help.
Sorry about that. Please use the code below instead:
<script>document.addEventListener("DOMContentLoaded", function() {const form = document.querySelector("#your-form-id"); // Replace with your actual form IDconst nightsField = document.querySelector("#nights-field"); // Replace with your nights field IDconst creditsField = document.querySelector("#credits-field"); // Replace with your credits field IDconst errorField = document.querySelector("#error-field"); // Replace with your error field IDform.addEventListener("submit", function(event) {const nights = parseInt(nightsField.value);const credits = parseInt(creditsField.value);if (nights > credits) {event.preventDefault();errorField.textContent = "Number of nights exceeds available credits.";}});});</script>The difference here is that instead of displaying the error in a dialog box that fires a form submission when OK is clicked, the error message is displayed in a html element (which could be a Span, Div).
amazing, thank you so much for the help!
A good way to handle this is by using Script #195 – Custom Input Validation. That script lets you add real-time checks and custom error messages to your Webflow form fields. In your case, you can compare the user’s input (requested nights) against the hidden Memberstack field (booking credits) before submission. If the requested nights exceed the available credits, #195 can stop the form from submitting and display a clear error message like “You only have 2 credits available — please adjust your request.”
That said, if your main need is more complex conditional logic (like multiple fields interacting or dynamic credit balances), you may need to extend #195 with a small custom JavaScript function that compares values and returns false when the condition isn’t met. Script #195 is best when you want real-time validation + friendly error handling, while a custom function may be necessary if you’re building more advanced conditional rules.
Please sign in to leave a comment.