If you want to add more specific form validation rules, you can do that using custom JavaScript!
How to Install in Unbounce
Click Here for Instructions
🚨
This is not an official Unbounce feature. This functionality is based entirely on third party code, and has only been tested in limited applications. Since this isn’t a supported Unbounce feature, our support team will be unable to assist if things go awry. So if you are not comfortable with HTML, Javascript and CSS, please consider consulting an experienced developer.
Part 1: Define a validation rule
Copy the following script and paste it into the Javascripts section of the builder:
Replace myCustomRule with a unique ID (name) for the rule. You’ll need this later.
Replace isValid with a function that takes the value as an argument and returns true if the value passes validation, or false if not. (The above example requires the user to enter ‘I accept’ exactly.)
Replace message with the error message that you would like to show when the user’s input does not match (optional)
Part 2: Apply the rule to form field(s)
Copy the following script and paste it into the Javascripts section of the builder:
Hi @bmiksich Unbounce actually has a built in honeypot field already included as a way to combat spam, so no need to worry about setting one up on your own. We don’t make it visible in the builder since it’s purpose is to filter out leads that fill it out. One of those behind the scenes features that isn’t really visible within the app.
I’m trying to get validation working on an entry that must be exactly 10 characters. The following will error, but will not succeed:
lp.jQuery(function($) {
// Config
var ruleID = 'promoCode';
var field = 'redemption_code';
var message = 'The code appears to be invalid';
var rules = module.lp.form.data.validationRulesefield];
I would suggest starting with one of the examples given by the Unbounce team at the top of this post and then add your regex.
There is some syntax you are missing and I think it would just be easiest to mimic one of the examples and go from there.
Thanks for helping, Kyle! I’m not really a coder, so that’s why I’m not sure where to insert this code into the base one from the example. If anyone could help, this would be great.
Oh okay,
Well keeping the code simple there is no if, else, that involves a mask. You can add this script to your page in the “Before Body End Tag” and it will validate based on the regex you included.
Hope that helps
Thanks a lot, Kyle! This would definetly helps!
Hi there,
I need some help to add a “Country Code” field to our landing pages on Unbounce, just before the Phone Number field. We are getting a lot of international inquiries and need to know their country code’s to be able to call them back.
Any help would be greatly appreciated.
I tried using this code and I get an error in the console and the validation is not working
Uncaught TypeError: Cannot read property ‘addMethod’ of undefined
at HTMLDocument. ((index):2992)
at Function.ready (lp-jquery.bundle-fc9ca0c.z.js:1)
at HTMLDocument.i (lp-jquery.bundle-fc9ca0c.z.js:1)
I am trying to validate a number range in a text field:
lp.jQuery(function($) {
var ruleID = 'range';
var field = 'valor_monetario';
var message = 'El valor debe ser entre $500,000 MXN y $20,000,000 MXN.';
var rules = module.lp.form.data.validationRulesufield];
$.validator.addMethod(ruleID, function(value, field) {
var valid = ( value >= 500000 && value <=20000000 )
return valid || (!rules.required && !value);
}, message);
rulesuruleID] = true;
});
Hey @claudias! For your specific implementation it may be the case that you’re calling the .validator method before (document).ready. Try including your script in a $(document).ready block.
It would look something like this:
$(document).ready(function () { /* your script*/ });
If that doesn’t work, we can keep digging!
that is instead of my first line right?
it would look like:
$(document).ready(function () {
var ruleID = 'range';
var field = 'valor_monetario';
var message = 'El valor debe ser entre $500,000 MXN y $20,000,000 MXN.';
var rules = module.lp.form.data.validationRulesefield];
$.validator.addMethod(ruleID, function(value, field) {
var valid = ( value >= 500000 && value <=20000000 )
return valid || (!rules.required && !value);
}, message);
ruleseruleID] = true;
});
right?
not working 😦
@claudias@mccamon Sorry, this one’s on us – we made a behind-the-scenes change a few days ago that is causing the errors you’re seeing. We’re testing a fix at the moment and are aiming to have that in place tomorrow. Sorry for any inconvenience this has caused. I’ll update this thread when the fix has been released.
@Mark_Wainwright Do you have any update on this? Seems every-time I get into work this problem is getting worse not better.
I currently have at least three different contingencies to deal with Unbounce not having loaded validation in early enough…
Just to further my last reply… I’ve been working at this for several hours today… then I noticed that a script I had deleted 4-5 hours before was still appearing in the final published version… (multiple saves, republishes, set to 1% republish set to 0% republish)…
I was then trying to explain to my superior exactly what was going on… then Refreshed not republished the page. (Keep in mind I’ve republished and refresh many times.) The script that I deleted earlier vanished, and the script I’ve been trying to debug suddenly started to work…
Is it worth my doing any work at the moment until I can be sure what I see in the landing page reflects what is in the editor?
Correct me if I’m wrong @Mark_Wainwright I believe the Unbounce team corrected the “behind-the-scenes” change this past Friday (7th of September) and scripts should now be working as they should.
You’re correct @Kyle.C, we pushed out a fix for this on Friday afternoon (apologies for not updating this thread sooner). You’ll just need to republish any affected pages, popups, or sticky bars.
Sorry again for the inconvenience – and please reach out to our support team if you experience any further issues.
I’m still getting the same error 😦
Uncaught TypeError: Cannot read property ‘addMethod’ of undefined