Adding custom validation to form fields


#41

I tried using this code and I get an error in the console and the validation is not working

Uncaught TypeError: Cannot read property ‘addMethod’ of undefined
at HTMLDocument. ((index):2992)
at Function.ready (lp-jquery.bundle-fc9ca0c.z.js:1)
at HTMLDocument.i (lp-jquery.bundle-fc9ca0c.z.js:1)

I am trying to validate a number range in a text field:

lp.jQuery(function($) {
	    var ruleID = 'range';
	    var field = 'valor_monetario';
	    var message = 'El valor debe ser entre $500,000 MXN y $20,000,000 MXN.';
	    var rules = module.lp.form.data.validationRules[field];
	    $.validator.addMethod(ruleID, function(value, field) {
	      var valid = ( value >= 500000 && value <=20000000 )
	      return valid || (!rules.required && !value);
	    }, message);
	    rules[ruleID] = true;
	 });

any idea? thanks!

This is my landing: https://negocio.iontucasa.com.mx/


#42

Hey @claudias! For your specific implementation it may be the case that you’re calling the .validator method before (document).ready. Try including your script in a $(document).ready block.

It would look something like this:
$(document).ready(function () { /* your script*/ });

If that doesn’t work, we can keep digging!


#43

that is instead of my first line right?
it would look like:

    $(document).ready(function () { 
    	    var ruleID = 'range';
    	    var field = 'valor_monetario';
    	    var message = 'El valor debe ser entre $500,000 MXN y $20,000,000 MXN.';
    	    var rules = module.lp.form.data.validationRules[field];
    	    $.validator.addMethod(ruleID, function(value, field) {
    	      var valid = ( value >= 500000 && value <=20000000 )
    	      return valid || (!rules.required && !value);
    	    }, message);
    	    rules[ruleID] = true;
    	 });

right?


#44

not working :frowning:


#45

@claudias @mccamon Sorry, this one’s on us – we made a behind-the-scenes change a few days ago that is causing the errors you’re seeing. We’re testing a fix at the moment and are aiming to have that in place tomorrow. Sorry for any inconvenience this has caused. I’ll update this thread when the fix has been released.


#46

@Mark_Wainwright Do you have any update on this? Seems every-time I get into work this problem is getting worse not better.
I currently have at least three different contingencies to deal with Unbounce not having loaded validation in early enough…


#47

Just to further my last reply… I’ve been working at this for several hours today… then I noticed that a script I had deleted 4-5 hours before was still appearing in the final published version… (multiple saves, republishes, set to 1% republish set to 0% republish)…
I was then trying to explain to my superior exactly what was going on… then Refreshed not republished the page. (Keep in mind I’ve republished and refresh many times.) The script that I deleted earlier vanished, and the script I’ve been trying to debug suddenly started to work…
Is it worth my doing any work at the moment until I can be sure what I see in the landing page reflects what is in the editor?


#48

@TimothyDO @claudias @mccamon

Correct me if I’m wrong @Mark_Wainwright I believe the Unbounce team corrected the “behind-the-scenes” change this past Friday (7th of September) and scripts should now be working as they should.


[SOLVED] Do you know why this stopped working?
#49

@claudias @mccamon @TimothyDO

You’re correct @Kyle.C, we pushed out a fix for this on Friday afternoon (apologies for not updating this thread sooner). You’ll just need to republish any affected pages, popups, or sticky bars.

Sorry again for the inconvenience – and please reach out to our support team if you experience any further issues.


#51

I’m still getting the same error :frowning:

Uncaught TypeError: Cannot read property ‘addMethod’ of undefined


#52

Hey @claudias

Where is the field named “valor_monetario” I don’t see that in your form or as any of the form Id’s.

The script is looking for that field to validate the entry, but it doesn’t exist and therefore is undefined.


#53

It is hidden until the user selects an option from the combo box, but it exists in the Dom


#54

@Kyle.C
I even added another js and has the same problem:
Uncaught TypeError: Cannot read property ‘addEventListener’ of undefined


#55

We’ve been having similar issues with numerous things now.
Main issue has been the custom validation we run for phone numbers from Data8. We get the AddMethod issue on that one. (pretty much the same as @cladius’s original issue)
We also use Selectize.js on some forms which we’ve had to get rid of. I also tried choosen.js which came up with the same error. (both widely recognised jquery plugins) Ended up falling back to vanilla html multi selects for that.
There is definitely some load order issues happening at the moment. Worst thing is that things are changing in the background so one day something breaks, I managed to get it running again, next day a differenent or similar issue starts.


#56

@claudias Adding a 500ms setTimeout fixed a similiar issue for us.


#57

I’ll try it! thanks!


#58

No coding experience here, but wondering if it was possible to exclude all emails from an email form field that are not “.org” or “.edu” email addresses. Anyone have any idea how to do this?

Thank you!


#59

Wondering if anyone has had any luck using this script to validate a URL in a field?


#60

@Mark_Wainwright
I’m experiencing this issue right now. ‘cannot set property myCustomHoneypot of undefined’ . In the console I look for the lp object and it simply isn’t there. What changed?


#61

Same for me