Adding custom validation to form fields


#21

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


#22

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


#23

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.


#24

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


#25

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


#26

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


#27

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.


#28

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


#29

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; 

});

[Tips and Scripts] Form Field Masking
#30

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


#31

Any help?

Thank you


#32

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.


#33

Hi @getreel,

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

Best,
Hristian


#34

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


#35

can anyone help?


#36

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.


#37

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.


#38

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


#39

Thanks a lot, Kyle! This would definetly helps!


#40

Hi there,

I need some help to add a “Country Code” field to our landing pages on Unbounce, just before the Phone Number field. We are getting a lot of international inquiries and need to know their country code’s to be able to call them back.

Any help would be greatly appreciated.