Skip to main content

Hello all!


I wanted to share a script I modified, that now prevents matching First + Last Name fields from being submitted. This script also works well if you’ve been receiving empty leads lately, as it turns out those leads aren’t in fact empty! After speaking with Bruna in Unbounce Support I was surprised to find out most fields marked as required (aside from email) can actually be bypassed by entering in a space, this results in what appears to be empty leads showing.


This script is meant to prevent submissions with matching first + last names, obviously we don’t see too many “Michael Michael” or “Susan Susan” names in the wild, yet we see spam of that nature! Since single spaces in each name field match, this will also prevent empty (spaced) lead submissions.


<script>

lp.jQuery(function($) {

var ruleID = 'FLnameMatch';

//The first name field to check against
var firstNameField = 'first_name';

//The second name field
var secondNamefield = 'last_name';

var message = 'Input Error, Please Contact Us';

var rules = module.lp.form.data.validationRulesasecondNamefield];

$.validator.addMethod(ruleID, function(value, secondNamefield) {

var nameValue = $('#' + firstNameField ).val();
var valid = ( value !== nameValue );

return valid || (!rules.required && !value);
}, message);

rulesrruleID] = true;

});

</script>

You can include this script on single landing pages by adding it to the Javascripts section when editing the page. If ALL of your forms on landing pages have First + Last Name you should also be able to add this globally instead of on every single landing page (avoiding the need for multiple page edits).


Credit where it’s due: I found the original script in this community, I meant to share the page URL with Bruna during our support chat but haven’t been able to locate my bookmark on it, so I can only take a little credit for the slight alteration and for realizing this works well on “spaced” first + last name empty lead submissions!


Hope this is helpful! If a moderator could please move this to Tips + Scripts category on here please, I think it would be very beneficial to others!

This is AMAZING @Michael_Miller!! I’ll move this into Tips and Scripts, thank you so much for sharing it here! This is tremendously helpful!


-Jess


Thank you Michael, I appreciate this.


This doesn’t seem to be working for me. Still lets through submissions with the same first name. Not sure what I’m doing wrong as the code snippet is pretty straight forward


Hi Trevor,

I have not yet tried this. I was contacted by Michael Miller about it - unbounce@discoursemail.com


The most common cause for these issues is if you’re running other javascript on your page, they can potentially cancel each other out. Additionally, multiple versions of jquery can cause scripts to break. Check on those, that may solve your problem 🙂


-Jess


Jess is right, sometimes it can be another Javascript running try changing the placement. If that doesn’t work, you could also try adding another script to define the jQuery version being used, for example, add another Javascript code to the landing page:


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>

On one landing page, another snippet of code included (for a third-party, marketing reasons etc) caused an issue where a script wouldn’t work until we changed the placement from “Head” to “After Body Tag” even though it worked fine being placed in the head on two other landing pages! I’ve also noticed what seems to be an inconsistent load order for scripts, to which I haven’t had time to look into (but this could be due to differentiating jquery versions being specified now that I see Jess’s response above - I’m very curious why on some landing pages that does seem to matter, especially in the script order sense).


Then there was one, where we had to specifically define the version inline with the script:


I can say, tinker with the placement, if you need to merge some scripts into the same editor, that should be fine as well.


@Michael_Miller This looks like exactly what I need!

I’m not sure whether I’m missing something but I can’t see where lp is declared? When I add this as a page variant script (including jQuery library, firing in the head) I received a console ‘undefined’ error for lp. If I assign lp to window.ub then the jQuery methods don’t work.

Do you have any idea where I’m going wrong so early on in the script?

I’m fairly new to Unbounce’s object model, and I can’t find any clear documentation on the reference to help me dig into the validator method.

~ FP Daisy 👍


Nevermind, I think I found a workaround!


I since saw this post:



Saying that jQuery was dropped as a dependency. I assume this is part of what was messing up the lp object variable and the later jQuery methods.


So I went back to the drawing board:



And modified your script to this:


<script type="text/javascript">

var lpf = window.ub.form;

//The first name field to check against
var firstNameField = 'forename';

//The second name field
var secondNamefield = 'surname';

//No matching first name / last name
lpf.customValidators.FLnameMatch = {
isValid: function(value) {
var fnv = document.getElementById(firstNameField).value;
var rules = lpf.validationRules.secondNamefield];
var valid = ( value !== fnv );

return valid || (!rules.required && !value);

},
message: 'You first and last names are the same, please check and try again.'
};
lpf.validationRules.secondNamefield].FLnameMatch = true;

</script>


And success! I think Unbounce automatically trims whitespace in form fields now, so I wrote another rule for spaces in fields but it turned out to be redundant.


Thanks for the great foundation for this script!


Edit:

One small update:


var valid = ( value.trim() !== fnv.trim() );

instead of


var valid = ( value !== fnv );

also catches same values with added whitespace.


Amazing work @Unbounce_FPD . Thanks for sharing 😮


Reply