Solved

Number Field Validation - Script

  • 17 April 2019
  • 4 replies
  • 52 views

Hi gang! A need some help to write a script. I am trying to validate a field that allow values from a range of numbers: 20 to 500.

How can I do it?

icon

Best answer by Nicholas 17 April 2019, 03:08

View original

4 replies

Userlevel 7
Badge +4

Hi, I think I figured out how to do this using a script that was shared on another thread.

Here’s a video showing what to do: https://www.loom.com/share/eb41687ce4804cacbdc253c9f24a58ce

Here’s the thread I referenced: Adding custom validation to form fields

And here’s the modified script I used:

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

Good luck!

Nicholas thanks so much for your support, it is really helpful. I was trying to do this for weeks 😦, without any luck. The script is working great. The implementation is really simple with the video instructions.

Thanks again my friend!

Userlevel 7
Badge +4

That’s great to hear!

Hi, Nicholas.

This dont fix on my page.

Why i do wrong?

http://www.kforte.com.br/teste2/ teste 1 is the field

Reply