`step` automatic rule gets broken for very small steps

Author: bderletaCreated Aug 17, 2023Updated Jan 29, 2025
LabelsType: BugStatus: Verified

Your environment

  • Version of jquery-validate: 1.19.5
  • Browser name and version: Firefox 116.0.2

Current behavior

When input[type="number"] has an attribute step with value of magnitude smaller than approx. 1e-7, the step rule has its value assigned in an exponential form, causing regular expression not to being matched.

Expected behavior

Proper input validation, taking actual number of decimal places into account.

Live demo

https://jsfiddle.net/ydj8o4nu/

Hotfix proposal

After reconsideration, I see it might not cover all possible cases, but alternate solutions like toLocaleString are even more dodgy.

decimalPlaces = function( num ) {
  var exponentialMatch = ( "" + num ).match( /(?:e\-(\d+))?$/ );
  if ( exponentialMatch && exponentialMatch[1] ) {
    return parseInt(exponentialMatch[1]);
  }
                
  var match = ( "" + num ).match( /(?:\.(\d+))?$/ );
  if ( !match ) {
    return 0;
  }
  
  // Number of digits right of decimal point.
  return match[ 1 ] ? match[ 1 ].length : 0;
  },

Source: jquery-validation/jquery-validation