Forced Field Validation



Show first post

53 replies

Userlevel 5

Oh okay, I understand. You want to use it with the birth year and not the number of years. Sorry I should’ve gotten that the first time 😉

The line below should will work. But keep in mind that it will have to be changed every year to include the new digit allowing 21 or older.

var valid=(/^([0-9]{2})\/([0-9]{2})\/(194[0-9]|195[0-9]|196[0-9]|197[0-9]|198[0-9]|199[0-8])$/.test(value));

I updated the regex and the error message now display on any age or date format: https://www.screencast.com/t/bMvjDlriqg5

<script>
lp.jQuery(function($) {
  var ruleID = 'birthday';
  var field = 'birthday';
  var message = 'Please enter a date in this format: MM/DD/YYYY';
  var rules = module.lp.form.data.validationRules[field];
  $.validator.addMethod(ruleID, function(value, field) {
        var valid = ( /^([2-8][1-9]|9[0-9]|[1-2][0-9]{2}|200)$/.test(value) );
        return valid || (!rules.required && !value);
        }, message);
rules[ruleID] = true;
});
</script>
Userlevel 5

Hey @erikleist

A simple fix is to edit the regex. The example below validates from 21-200

var valid = ( /^([2-8][1-9]|9[0-9]|[1-2][0-9]{2}|200)$/.test(value) );

Hope that helps 🙂

This is great and I just used it. Can you think of a quick way in this format to also verify the age of the individual is at least 21?

Userlevel 5

Perfect! Glad you got it working 🙂

Thanks Kyele! Somebody from the group pass me a script. It is working great. The script is:

<script>
  
  lp.jQuery(function($) {
  
    var ruleID = 'custom';
    var field = 'number';
    var message = 'Please enter a valid number';
  
    var rules = module.lp.form.data.validationRules[field];
  
    $.validator.addMethod(ruleID, function(value, field) {
  
      var valid = ( /^([2-8][0-9]|9[0-9]|[1-4][0-9]{2}|500)$/.test(value) );
  
      return valid || (!rules.required && !value);
    }, message);
  
    rules[ruleID] = true;
  
  });
</script>
Userlevel 5

Ah I see what you need. That requires a more detailed line of regex. Replace that var valid with the following and it will work for number between 20 and 500.

var valid = /(?<!-)\b([2-9][0-9]|1[0-9][0-9]|2[0-9][0-9]|3[0-9][0-9]|4[0-9][0-9]|500)\b/.test(value);

I’m a bit of a rookie with regex logic so let me know if that works on your page!

Thanks so much Kyle! I do not what I am doing wrong. It is not working. This is what is showing in the webpage after I add the script.

Userlevel 5

I’ve made some edits to the var valid…The script below should work. You can change the field name and message as needed in your case.

< script>
lp.jQuery(function() {
var ruleID = 'dob' var field = 'date_of_birth';
var message = 'Please enter a date in this format: 123';
var rules = module.lp.form.data.validationRules[field];
.validator.addMethod(ruleID, function(value, field) {
var valid = /^([0-9]{3})$/.test(value);
return valid || (!rules.required && !value);
}, message);
rules[ruleID] = true;
});
</script>

Hi Kyle! How can I validate to allow just 3 digit number?

Hi!
The work email is working just fine!
But is there a way to valid a text form an array?
Example: Show error message when the text is not “text 1” or “text 2” or “text 3”

Thanks!

Userlevel 5

@blynch1712

Your Date of Birth field name and ID is “dob” and it needs to be “date_of_birth”

Thanks! The variant I am pointing traffic to is http://www.disabilitybenefitnetwork.com/application/

I’d prefer to update the javascript on that page, so let me know if you can figure out what the issue is. Let me know if you need any further info. Thanks again!

Userlevel 5

If you provide the URL to the page you run traffic to, I can look and see what may be going wrong.

Alternatively as a work-around, you could move your “SSD TEST Copy” variant and add it to the page you run traffic to, then point everything to that variant 😉

It was a filtering issue. Found the page.

I did make the updates on the SSD Test Copy page and tested it and all is working correctly. I copied the code over to the page I’m running traffic to and it’s not working there. I saved and republished and cleared my cache and I’m not seeing the errors on either the DOB or the Zip Code field. I went back and resaved a second time and I’m still not having any luck. Is there any further updates I need to make? Thoughts?

Userlevel 5

Well I really doubt that it’s gone, unless you consciously selected to delete the page or “discard” the variant and then click again to “delete” the variant.

Let’s start with clarifying the difference between a page and a variant in Unbounce. A single page can have multiple variants in which all the variants point to the same URL, based on the weight you assign them.

Surly, your “SSD Copy” page has the variant that you have been working on within it.

These are pages

Capture

These (highlighted) are variants

Capture2

Correct. When signing in I see the active page that I have directed traffic to and the SSD Copy I initially created off the original. However, the new variant I created with the URL: http://www.disabilitybenefitnetwork.com/ssd-test-page/ to specifically test outside of any traffic, I cannot seem to find in the unbounce interface. Thoughts?

Userlevel 5

Sorry, I need some clarification…so you can’t find the page you have been working on in your Unbounce account?

Hi Kyle - when signing back in to unbounce to retest my test URL, I can’t seem to find it. Again, probably a silly question, but can you point me in the right direction? I created the new test URL but it’s not showing as existing and I did initially publish it to test. The URL is still published because I can look at it directly.

http://www.disabilitybenefitnetwork.com/ssd-test-page/

Thoughts?

Userlevel 5

Ah sorry @blynch1712,

When I pasted the dob script here in the community it removes some characters! 😬
I guess that’s why it’s safer to go with github.

Replace the former dob script with this one here.

Great, thanks! I created a test page off of that variant I created.

http://www.disabilitybenefitnetwork.com/ssd-test-page/

However, the DOB doesn’t appear to be throwing up an error when an incomplete DOB is entered. Any idea what might be wrong there?

Userlevel 5

Oh I see,

Perhaps then set up a new “test” page that isn’t tied to anything else like your domain and add that variant to it.
Unbouncepages.com/my-test-page”

I think there is an option to create a new page from variant or something along those lines to make things easier.

Will I run the risk of the traffic hitting the new variant/page that isn’t setup correctly though? Or I am misunderstanding what you are saying? Basically, I’d like to just run some test leads through the new variant without it affecting traffic on the existing variant. Thoughts?

Userlevel 5

There is an option to see what your page looks like in the builder.

Look in the top right area of the window for… “👁️ Preview”

That will allow you to see your selected variant before it’s live. But sometimes I’ve noticed scripts that don’t fire off in the preview. It’s always best to test things like this from the live URL. If you need to do that, just add 100% of your traffic to the new variant from the overview page. This way you know you will land on the proper variant to test.

Good morning! So I’ve made the duplicate page (“SSD copy 1”), adjustments to the fields and added the scripts you provided and here’s my silly question… how can I test just this variant specifically? I don’t want to add any traffic to it at this time, I’d just like to run through and make sure the updates are working as expected. Can you point me in the right direction? I’ve been looking around and can’t seem to find what I’m looking for.

Thanks!

~B

Reply