Adding custom validation to form fields



Show first post

112 replies

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

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

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.

can anyone help?

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

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

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.

Any help?

Thank you

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

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; 

});

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

Userlevel 6

Hi @bmiksich Unbounce actually has a built in honeypot field already included as a way to combat spam, so no need to worry about setting one up on your own. We don’t make it visible in the builder since it’s purpose is to filter out leads that fill it out. One of those behind the scenes features that isn’t really visible within the app.

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

Userlevel 2
Badge +1

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

Userlevel 3

Hey Nayon

It looks to be working for me

I assume you got it figured out, but let me know if you’re still having issues with it

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.

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

Userlevel 7
Badge +4

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

Userlevel 7
Badge +3

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

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

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

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

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

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?

thank you Hristian, now it work!

Best,

J

Reply