Adding custom validation to form fields

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

Hello,

I was wondering if Unbounce’s decision to remove jQuery from the landing pages would affect the validation scripts shared in this thread.

If Unbounce is removing jQuery from the lp object then what would be their alternative rather than using jQuery’s .validator plugin which is what is currently recommended?

Any further direction would be appreciated.

Thank you

  • Jon S.
3 Likes

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!

Ok, seriously, why are we still custom coding form validation for really simple requests, like simply validating something is a number. This is really frustrating.

Secondly, the instructions at the top of this don’t seem to work. Or are confusing enough that they are difficult to implement. This seems pretty straight forward:

<script>
window.module.lp.form.data.validationRules['field_name'].digits = true;
</script>

But no. It isn’t. The instructions don’t tell you where to include it: head, after body, before body, etc. Although that doesn’t matter because I’ve tried them all and it doesn’t work.

Of course, I’ve seen a reference in this thread to JQuery no longer being supported or plans to remove it, so I don’t know if this thread is still valid, except that I chatted with customer support and this is what they pointed me to.

I’m beyond frustrated with this.

3 Likes

Hi guys,

Unfortunately, web traffic isn’t always great. Especially when you’re receiving a lot of spam leads. With the following scripts you can at least block non work email addresses.

//Placement: Before Body End Tag

<script>
  window.ub.form.customValidators.nonWebmailEmail = {
  isValid: function(value) {
  return /(@(?!me|mac|icloud|gmail|googlemail|hotmail|live|msn|outlook|yahoo|ymail|aol).)/.test(
  value.toLowerCase()) && /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(
  value.toLowerCase());
},

  message: "Please enter your work email address"
};
</script>

and…

//Placement: Before Body End Tag

<script>
  window.ub.form.validationRules.email.nonWebmailEmail = true;
</script>
2 Likes

Hi, I’m trying to add custom field validation to my form but it’s a multi-step form built with this method (Multi-Field, Multi-Step Form 🔥) and I suspect it doesn’t work because of that… Any input?

1 Like

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

@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!!

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/

Hey @Eline_M !

Did you find a solution to your problem ?

Hi @Mark_Wainwright, is it possible to get the email validation script to also validate that the entry includes a “domain extension”? With the current script you can submit emails without .com or similar - in other words invalid emails.

1 Like

yes, that would be a good idea.
i still regularly get emails without domain extension

1 Like

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) :wink:

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 :slight_smile: . 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.