Forced Field Validation


I’m looking to find a way to force field validation on 3 different fields on my page:

Phone Field: XXXXXXXXXX (must be 10 numeric values only)
Zip Code: XXXXX (must be 5 numeric values only)
DOB: must be MM/DD/YYYY format only

Help!!


53 replies

Thank you! It looks like it’s working now when I preview the page!

Userlevel 5

Okay I think there is an issue with the old jQuery. Try using this script instead.

<script>
window.ub.form.customValidators.validatedob = {
isValid: function(value) {
  return /^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/.test(value);
},
message: 'Please enter a date in this format: MM/DD/YYYY',
};
</script>
<script>
window.ub.form.validationRules.date_of_birth.validatedob = true;
</script>

Referencing this post in the community:

Of course - here’s the URL. It’s page 2 of a multi-step form. I’m just looking to have the DOB form make sure users enter it as MM/DD/YYYY, since that’s how the data needs to be fed into the CRM system.

https://quote.t65medicareenroll.com/step-two-1/

Here’s the URL for the first page in the group, just for reference.

https://quote.t65medicareenroll.com/get-started-1/

Userlevel 5

Could you include the URL to the page so we can inspect?

It might be helpful to see what is going on within the page.

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?

Userlevel 7
Badge +3

Hi @lasuma12,

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

Best,
Hristian

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

Well, this is because of the date format. The following regular expression is validating dd/mm/yyyy date format:
return /(((0|1)[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))$/.test(value);

To obtain a MM/DD/YYYY, you must change the regular expression to:
return /((0[1-9]|1[0-2])\/((0|1)[0-9]|2[0-9]|3[0-1])\/((19|20)\d\d))$/.test(value);

As well, make sure you change your error message to :
"Enter date in mm/dd/yyyy format ONLY."

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.

Sure. Here it is

<script>
    window.ub.form.customValidators.checkDobFormat = {
        isValid: function(value) {
          return /(((0|1)[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))$/.test(value);
        },

        message: 'Enter date in dd/mm/yyyy format ONLY.',
        };

      window.ub.form.validationRules.date_of_birth.checkDobFormat = true;
</script>

It uses the core methods of the ub object (customValidators).

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?

image

In Chrome, right click on your page and click on Inspect (CTRL+SHIFT+I on PC). It will open a panel at the bottom or the right of your Chrome browser. Then select the Tab Console and reload the page. If there is something wrong in your JS the error will show there.

Thanks

Xav

@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

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

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.


Userlevel 5

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

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

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 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

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

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

Badge

No worries, I found the solution here: http://regexlib.com/Search.aspx?k=dd%2Fmm%2Fyyyy&c=-1&m=-1&ps=20

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?

Userlevel 5

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

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?

Reply