Last active
November 1, 2019 11:08
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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