Number Field Validation - Script

#1

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?

0 Likes

#2

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!

2 Likes

#3

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

Thanks again my friend!

2 Likes

#4

That’s great to hear!

0 Likes