Forced Field Validation



Show first post

53 replies

Userlevel 5

@blynch1712

Your Date of Birth field name and ID is “dob” and it needs to be “date_of_birth”

Hi!
The work email is working just fine!
But is there a way to valid a text form an array?
Example: Show error message when the text is not “text 1” or “text 2” or “text 3”

Thanks!

Hi Kyle! How can I validate to allow just 3 digit number?

Userlevel 5

I’ve made some edits to the var valid…The script below should work. You can change the field name and message as needed in your case.

< script>
lp.jQuery(function() {
var ruleID = 'dob' var field = 'date_of_birth';
var message = 'Please enter a date in this format: 123';
var rules = module.lp.form.data.validationRules[field];
.validator.addMethod(ruleID, function(value, field) {
var valid = /^([0-9]{3})$/.test(value);
return valid || (!rules.required && !value);
}, message);
rules[ruleID] = true;
});
</script>

Thanks so much Kyle! I do not what I am doing wrong. It is not working. This is what is showing in the webpage after I add the script.

Userlevel 5

Perfect! Glad you got it working 🙂

This is great and I just used it. Can you think of a quick way in this format to also verify the age of the individual is at least 21?

I updated the regex and the error message now display on any age or date format: https://www.screencast.com/t/bMvjDlriqg5

<script>
lp.jQuery(function($) {
  var ruleID = 'birthday';
  var field = 'birthday';
  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 = ( /^([2-8][1-9]|9[0-9]|[1-2][0-9]{2}|200)$/.test(value) );
        return valid || (!rules.required && !value);
        }, message);
rules[ruleID] = true;
});
</script>

Oh very cool! Worked and created a calendar reminder for Jan 1 🙂

Do you know if there’s any way to make this scrip AMP compliant?

Userlevel 5

AMP will be releasing script functions conditionally soon. Right now, there is no way to use custom scripts for AMP.

Badge

Hi Kyle, if I understand it correctly, this script only makes sure the date is in 12/12/1222 format - it only checks the quantity of numbers (only possible to use 0-9) between the slash symbols. For my client I need to force validation of the date in UK format - dd/mm/yyyy. So that it is not possible to type in date such as 09/31/2022 (US date format) since there are only 12 months in a year.
So it should force this change 31/09/2022

Can you help me with that please?

PS: My field ID and therefore var field in the code is ‘date_of_your_event’, do I need to change the ruleID somehow as well?

I followed the exact same steps and it is not throwing any errors when I fill the DOB field incorrect.

Does anyone know of the script still works?

Thanks

Userlevel 5

Hey @Ronald_Tu I’m happy to help. Could you share the URL to the page?

Thanks Kyle!

The idea is to force the input date value to match the required mailchimp date format. This way I could setup the api connection between Unbounce and Mailchimp.

http://unbouncepages.com/testron/

Userlevel 5

Okay I think it’s a simple fix!

It’s an older script so you need to make sure the following 2 scripts are in the page:

jQuery (1.4.2)

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

jQuery.Fancybox (1.3.4)

<link href="//assets.unbounce.com/m/lp-webapp/jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.css" media="screen" rel="stylesheet" type="text/css">
<script src="//assets.unbounce.com/m/lp-webapp/jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.js"></script>

Thanks for your response Kyle.

But it doesn’t seem to work, I added a screenshot of where I pasted the jQuery scripts you provided. Did I do it right?

Userlevel 5

Make sure the placement for the validator function is “Before Body End Tag” not in the “Head.”

I made the change to “Before Body End Tag”, but still not able to get it working. Here is a screenshot so you can see my script for the DD/MM/YYYY script.


@Ronald_Tu, A quick check… in your Chrome do you see the error “lp is not defined?”

@xavier.bussonnais - Where would find that error? I don’t see anything like that on my browser window. Could you screenshot where the error “Ip is not defined” would show up?

Thanks,
Ron

Userlevel 5

The page URL is shared at the top of the thread. It looks like he ran into the same problem you had.

@xavier.bussonnais could you share the script you edited to get it working?

Hey @xavier.bussonnais, this seem to have worked. But Now I am running into another error, when I put in the proper date format in, it is firing off the error message.

Hi! I am trying to use this script to validate date of birth in my form as (MM/DD/YYYY) and can’t get it to work. The validation error won’t show up when I’ve tested it out using the incorrect formatting.

Below are screenshots of the scripts I’ve placed on the landing page, as well as my form setup.

Screen Shot 2020-05-21 at 11.56.33 AM|690x141

Screen Shot 2020-05-21 at 12.06.22 PM|690x425

Userlevel 7
Badge +3

Hi @lasuma12,

This is probably because the jQuery script is being loaded after your validation script.

Best,
Hristian

Hi! Thank you for your response! I moved the jQuery script to the Head of the page and republished. When I tested, the verification for age still didn’t show an error when I typed it it incorrectly.

Should the Form Field type be a Single-Line Text or something else? I’ve followed all of the steps above as far as I can tell. Not sure why this isn’t working. Is there any other solution for this?

Reply