Skip to content

Instantly share code, notes, and snippets.

@KyleCarline
Last active November 1, 2019 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KyleCarline/2d94b6a1591b4caf120abd08011a639b to your computer and use it in GitHub Desktop.
Save KyleCarline/2d94b6a1591b4caf120abd08011a639b to your computer and use it in GitHub Desktop.
Validate Date of Birth in an Unbounce page form field to read MM/DD/YYYY
<script>
lp.jQuery(function($) {
var ruleID = 'dob';
var field = 'date_of_birth';
var message = 'Please enter a date in this format: MM/DD/YYYY';
var rules = module.lp.form.data.validationRules[field];
$.validator.addMethod(ruleID, function(value, field) {
var valid = /^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/.test(value);
return valid || (!rules.required && !value);
}, message);
rules[ruleID] = true;
});
</script>
@xavierbuss
Copy link

Hi,
I tried to use this script, but without success. Either "lp." is undefined, either I get this answer "Uncaught TypeError: Cannot read property 'addMethod' of undefined".

I have added the below librairies in the script manager :

<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/additional-methods.min.js"></script>

What "lp." is referring to exactly ?

Thanks in advance for your precious help !

Xavier

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment