Adding custom validation to form fields



Show first post

112 replies

Hi @Hristian,

I’m trying to use the above script to validate two radio buttons on my landing pages form. The radio button options are “YES” and “NO”. I need the lead to click on the “YES” option in order to proceed and successfully click the CTA button, registering with us. However, I’ve tried dozens of variations of the script found on the unbounce community pages but none of them seem to work! I really need some help with this one!!

Userlevel 7
Badge +3

Hi @nico,

You’ll need to modify both of your scripts - the multi-step and the custom validation to work together.

Best,
Hristian

Hi @Hristian

Thanks for getting back to me. Unfortunately I’m a complete noob at coding with Jquery. Is there anyway I could hire a freelancer to do this for me through the unbounce community?

Cheers,
Nico

Could you give an example of this for a custom message that displays whenever someone selects a “Yes” radio button in a group and shows no message when someone selects the “No” radio button in the same group.

I’m trying to make give a custom message when someone selects “Yes” for each of 2 separate radio button questions.

Claudia me puedes ayudar a generar un script que valide range de 1 a 500? gracias!

Hi @felipe_vimcar - I am trying to implement this but the script to block non work email adresses but it doesn’t seem to register. Do the two bits of script need to be added separately? Thanks

Userlevel 1

@a.furness
Yep, they need to be added separately!

The US Zip code Validation is not working, can someone help me?

I’m in the same boat, if anyone has a way to get US Zip Code validation working I would really appreciate the help!!

Badge

I have tried the US Zip Code validation script in the original post. It’s not accepting any answer in that field even though I’ve entered a valid zip code.

(before body end tag)

<script>
  window.ub.form.customValidators.usZipCode = {
    isValid: function(value) {
      /^\d{5}([\-]?\d{4})?$/.test(value);
    },

    message: 'Please enter a valid US ZIP code',
  };
</script>

and also (before body end tag)

 <script>
   window.ub.form.validationRules.zip.usZipCode = true;
 </script>

Any suggestions on getting this to work? Thanks!

Did you add these to the Script Manager or to each individual landing page’s custom javascript? Thanks!

Hi, i’m facing this issue when i run my code below:

<script>
  window.ub.form.customValidators.validacao_telefone = {
    isValid: function(value) {
      /(\(\d{2}\)\s)(\d{4,5}\-\d{4})$/.test(value);
    },

    message: 'Informe um telefone válido',
  };
</script>

“TypeError: window.ub.form is undefined”

My page: http://unbouncepages.com/amildental/

Userlevel 5
Badge +4

Hey @Eline_M !

Did you find a solution to your problem ?

I’ve already put in feature requests on solving this… problem is that it’s a valid email without the domain… e.g. test@localhost or employee@companyintranet . so by using html5 email validation these are technically correct although completely useless for a internet only system.

Our solution goes something like:
document.getElementById('email_field_id').setAttribute("pattern", "[a-zA-Z0-9.-_]{1,}@[a-zA-Z0-9.-]{1,}[.]{1}[a-zA-Z0-9]{2,}");

Drop the above into a script and change email_field_id to the id of the email field on your form.

This uses html5 regex pattern matching to use as validation for valid emails (with domain) 😉

HEllo, Can you send me page file with validate postall code. It will be helpful.
Thank you sir.

Hi there Enayet!

Tas from Unbounce Support here - I’m happy to take a look for you 🙂 . Do you mind sharing a link to your landing page and I can see what you have so far?

Here is an example test page of mine with the Postal Code Validation: http://unbouncepages.com/testing-tas-form-validation-email/

To re-cap, you will need to set up two scripts with the placement for both set to “Before End Body Tag”.

If you’re still having issues, just thread here, or reach out to support via support@unbounce.com.

Hi Guys,

I put that script buth dont fix.

<script>
lp.jQuery(function($) {
         
           // Config
         var ruleID = 'OnlyNumber';
         var field = 'teste1';
         var message = 'Only Number';
         
         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; 

});
</script>

why?

hello, for some reason, the above “confirm email” scripts to validate two email fields was not working for me. This one to be exact: https://gist.github.com/johnnyopao/c9965db11cded79aede6

In the console (in the Chrome “Inspect” section), I kept getting an error saying “lp is not defined”. I wrote another script based on the original which does the same thing, but does not include lp.jQuerry. this script checks two emails, but can be applied to any two fields that you want to be the same

<script>
window.ub.form.customValidators.emailCheck = {
    isValid: function (value) {

        var firstEmailField = 'email'
        var secondEmailField = 'email_'


        var emailValue = $('#' + firstEmailField ).val();
        var emailValue2 = $('#' + secondEmailField ).val();

        if (emailValue === emailValue2) {
            return value;
        }
    },
    message: 'email addresses do not match',
};</script>

<script>
window.ub.form.validationRules.email.emailCheck = true; </script>

few things to note:
-the “emailCheck” in this line “window.ub.form.customValidators.emailCheck” should be changed to your unique ID for whatever function you’re doing (and everywhere else it is referenced).
-the two identifiers in these lines "var firstEmailField = ‘email’ var secondEmailField = ‘email_’ " should be changed to the two field names and identifiers that you want to compare.
-change the message to whatever you want
change the “email” in “window.ub.form.validationRules.email.emailCheck = true;” to whatever field you want the message to pop up on.

copy and paste this into your scripts section and it should work.

I have finally worked on a solution to validate Indian phone number and it’s live on the landing page.

The complete step by step process is here

There are 3 elements for this:

1) Regex for Indian phone number validation

^[6-9]{1}[0-9]{9}$/g

2) Unbounce Javascript 1

 <script> window.ub.form.validationRules.mobile.indianPhoneNumber = true; </script>

3) Unbounce Javascript 2

<script>
  window.ub.form.customValidators.indianPhoneNumber = {
    isValid: function(value) {
      return /^[6-9]{1}[0-9]{9}$/g.test(value);
    },

    message: 'Please enter a 10 digit phone number',
  };
</script>

Maybe this has already been answered - but if the field is left blank, the validation doesn’t work.

For example, if I set the required value to be “123”, if the user simply leaves the form blank and hits submit the validation doesn’t work and the form is submitted.

Any suggestions?

Userlevel 7
Badge +3

Hi @Evan-Dario,

Have you tried making the field required?

You can’t submit an empty field that’s also required.

Best,
Hristian

Hi,
I have a email field and if I enter invalid email(test@test.com) into that, i am populating a value(invalid) into a hidden field(another field). If hidden field value has “invalid”, i need to show a error message and need to prevent form submission.

Below is my code:

window.ub.form.customValidators.Email_Verification_Status__c = {
isValid: function (value) {
var firstEmailField = ‘Email_Verification_Status__c’
var emailValue = $(’#’ + firstEmailField ).val();
if (emailValue == “valid”) {
return value;
}
},
message: ‘Not a valid email address’,
};

Any help would be great.

Yes, I can help you…

@Enayethazari1,

Please check my code is this correct? Or Just update the code and place that here. So that I can use it for my page.

@Enayethazari1

Could you please help on this? This is a critical item for me.

Reply