Form Validation for a range of numbers that include decimals

  • 18 November 2021
  • 0 replies
  • 2 views

I’m wanting my form to validate numbers that include decimals.

Currently, we are requesting GPA so the form will need to validate numbers from 0-4.
The number can be 0.25 or 4.20 or 3 or 3.0.

This is the piece of JS I’m currently using and is placed Before the Body End Tag:

<script>
  
  lp.jQuery(function($) {
  
    var ruleID = 'custom';
    var field = 'gpa';
    var message = 'Please enter a valid GPA value. (IE - 3.5)';
  
    var rules = module.lp.form.data.validationRules[field];
  
    $.validator.addMethod(ruleID, function(value, field) {
  
      var valid = ( /^([0-4])\d{0,9}(\.\d{1,3})?%?$/.test(value) );
  
      return valid || (!rules.required && !value);
    }, message);
  
    rules[ruleID] = true;
  
  });
</script>

Here’s the link to the page: http://unbouncepages.com/gpa-validation-test/

Any help on this will be greatly appreciatedddd!!!


0 replies

Be the first to reply!

Reply