Adding custom validation to form fields



Show first post

112 replies

Badge

Hi

I’m trying to add a validation message if a certain (hidden) field has a value lower then 5. The value in the hidden field is set automatically if visitors select a picture on my page. The hidden field is ‘havebook’. I read the all above posts and tried to compose a code, but unfortunately it doesn’t work. 

Can anyone help me, please?

 

<script>
  
  lp.jQuery(function($) {
  
    var ruleID = 'custom';
    var field = 'havebook';
    var message = 'Select minimum 5 books';
  
    var rules = module.lp.form.data.validationRules[field];
  
    $.validator.addMethod(ruleID, function(value, field) {
  
      var valid = ( /value >= 5) );
  
      return valid || (!rules.required && !value);
    }, message);
  
    rules[ruleID] = true;
  
  });

</script>
 

Userlevel 7
Badge +3

Hi Joe, 

It turns out you need the () parentheses but also to wrap the RegEx in slashes / /.

I just tested it on this page and it seems to work: http://unbouncepages.com/custom-validation-test/

Here is the exact script:

&nbsp;&nbsp;<br />&nbsp; lp.jQuery(function($) {<br />&nbsp;&nbsp;<br />&nbsp; &nbsp; var ruleID = 'custom';<br />&nbsp; &nbsp; var field = 'validation_rule_test';<br />&nbsp; &nbsp; var message = 'Please enter a valid number';<br />&nbsp;&nbsp;<br />&nbsp; &nbsp; var rules = module.lp.form.data.validationRules[field];<br />&nbsp;&nbsp;<br />&nbsp; &nbsp; $.validator.addMethod(ruleID, function(value, field) {<br />&nbsp;&nbsp;<br />&nbsp; &nbsp; &nbsp; var valid = ( /[a-zA-Z]{6}[0-9]{2}[a-zA-Z][0-9]{2}[[a-zA-Z][0-9]{3}[a-zA-Z]/.test(value) );<br />&nbsp;&nbsp;<br />&nbsp; &nbsp; &nbsp; return valid || (!rules.required && !value);<br />&nbsp; &nbsp; }, message);<br />&nbsp;&nbsp;<br />&nbsp; &nbsp; rules[ruleID] = true;<br />&nbsp;&nbsp;<br />&nbsp; });<br />

If you make the above changes, it should work. 

Best,
Hristian

P.S. Make sure you adjust the ruleID, field and message.

thank you Hristian, now it work!

Best,

J

Userlevel 7
Badge +4

Hi, my client would like to modify this so that it validates an age entered into a text field, to confirm that the visitor is over a certain age. How can they do that?

Userlevel 7
Badge +3

Hi Nicholas, 

You just need to write a valid regex expression for whatever age range you want to validate against (ex. 18-99, 21-35, 19-66, etc.)

What’s your minimum and maximum allowed age?

Best,
Hristian

Userlevel 7
Badge +4

Hey, thanks Hristian! The age range is just 18 and older. Do you know what the exact expression would be for that?

Userlevel 7
Badge +3

Hey Nicholas, 

My regex expressions are a bit rusty but this should work (just make sure you test it):

(1[89]|[2-9]\d)

Best,
Hristian

EDIT: That is unless you are expecting leads older than 99 years. 🙂

Userlevel 7
Badge +4

Do you mind showing me where to plug this in? I’m using the script you posted above as a starting point, but I need to modify it to add in the regex you created.

&nbsp;&nbsp;<br /></pre><pre>&nbsp; lp.jQuery(function($) {<br />&nbsp;&nbsp;<br />&nbsp; &nbsp; var ruleID = 'custom';<br />&nbsp; &nbsp; var field = 'validation_rule_test';<br />&nbsp; &nbsp; var message = 'Please enter a valid number';<br />&nbsp;&nbsp;<br />&nbsp; &nbsp; var rules = module.lp.form.data.validationRules[field];<br />&nbsp;&nbsp;<br />&nbsp; &nbsp; $.validator.addMethod(ruleID, function(value, field) {<br />&nbsp;&nbsp;<br />&nbsp; &nbsp; &nbsp; var valid = ( /[a-zA-Z]{6}[0-9]{2}[a-zA-Z][0-9]{2}[[a-zA-Z][0-9]{3}[a-zA-Z]/.test(value) );<br />&nbsp;&nbsp;<br />&nbsp; &nbsp; &nbsp; return valid || (!rules.required && !value);<br />&nbsp; &nbsp; }, message);<br />&nbsp;&nbsp;<br />&nbsp; &nbsp; rules[ruleID] = true;<br />&nbsp;&nbsp;<br />&nbsp; });<br />
Userlevel 7
Badge +3

Make sure you change the “var field” & “var message” to reflect what you want.

Userlevel 7
Badge +4

Worked like a charm! I can’t thank you enough, Hristian!

How would this work for multiple checkboxes to validate that at least one checkbox must be selected?

Userlevel 2
Badge +1

Hey Johnny,
I tried using the code below for a Full Name validation in a single input field with a space between the texts. But for some reasons I couldn’t make it work! Will you please kindly have a look at it?

This is the code:

And this is my landing page: http://unbouncepages.com/pradaxa-one/

Thanks.

Userlevel 2
Badge +1

Yeah, It works fine now. actually I updated the script!

How could I modify this script to target a hidden field? I’m able to get it working on visible fields, but not hidden.

Specifically, I’m trying to create a blank field (honeypot) to stop bot traffic from submitting forms. The value of the hidden field should remain blank. If it is not blank, the form will not submit.

Thanks

I’m trying to get validation working on an entry that must be exactly 10 characters. The following will error, but will not succeed:

lp.jQuery(function($) {
         
           // Config
         var ruleID = 'promoCode';
         var field = 'redemption_code';
         var message = 'The code appears to be invalid';
         
         var rules = module.lp.form.data.validationRules[field];

          $.validator.addMethod(ruleID, function(value, field) { 
          
          var valid = ( /^[A-za-z0-9]{10}$/i.test(value) );
        }, message); 
  
  rules[ruleID] = true; 

});

Any help would be appreciated.

Thanks,
Brian

Figured it out.

lp.jQuery(function($) {
         
           // Config
         var ruleID = 'promoCode';
         var field = 'redemption_code';
         var message = 'The code appears to be invalid';
         
         var rules = module.lp.form.data.validationRules[field];

          $.validator.addMethod(ruleID, function(value, field) { 
          
         var exp = new RegExp('^[A-za-z0-9]{10,11}$', 'i');
		 return value.match(exp) ? true : false;
        }, message); 
  
  rules[ruleID] = true; 

});

Hello there,

I’m tryng to have a field that only should acept 3 letters or 4 letters.
If less than 3 letters should give error and if more than 4 letters should give error.

What is missing?

var valid = ( /[a-zA-Z]{3}|[a-zA-Z]{4}/.test(value) );

Thank you

Any help?

Thank you

Hello,

I’m looking for a digit only field in unbounce and I’m having some trouble.

i used:

I updated the field ID to reflect the one I wanted to be digits only but it didnt work.

Can somebody please help advise.

Userlevel 7
Badge +3

Hi @getreel,

Can we see the page you are working on so we can try to troubleshoot this for you?

Best,
Hristian

Hi guys.
Is there a way to make this code works on my page?
It should validate a brazilian mobile phone number and use a mask to help the user fill the form correctly.
Here’s the script that I have to put on unbounce:

<script src="http://digitalbush.com/wp-content/uploads/2014/10/jquery.maskedinput.js"></script>

<script>
ijQuery(document).ready(function () {
    var fieldLabel = 'ddd__telefone';
    ijQuery.validator.addMethod('validatePhone', function (value, element) {
        var phoneNoRegex = /^\+?\d{2}?\s*\(\d{2}\)?\s*\d{8,9}/;
        if (value.match(phoneNoRegex)) {
            return true;
        } else {
            return false;
        }
    });
    $('form [name="' + base64_encode(fieldLabel) + '"]').mask("+55 (99) 99999999?9");
    ijQuery('form [name="' + base64_encode(fieldLabel) + '"]').addClass('validatePhone');
    ijQuery.extend(ijQuery.validator.messages, {
        validatePhone: ': Inserir um telefone válido.'
    });
 });
</script>

My Field ID is the “ddd__telefone”.

thanks

can anyone help?

Userlevel 5

I would suggest starting with one of the examples given by the Unbounce team at the top of this post and then add your regex.

There is some syntax you are missing and I think it would just be easiest to mimic one of the examples and go from there.

Thanks for helping, Kyle! I’m not really a coder, so that’s why I’m not sure where to insert this code into the base one from the example. If anyone could help, this would be great.

Userlevel 5

Oh okay,

Well keeping the code simple there is no if, else, that involves a mask. You can add this script to your page in the “Before Body End Tag” and it will validate based on the regex you included.

Hope that helps

Reply