Regular verification method that can only have two digits after the decimal point

  • 2021-07-24 09:45:04
  • OfStack

Clear as long as there is the third place


<input type="text" onkeyup="value=value.replace(/\.\d{2,}$/,value.substr(value.indexOf('.'),3))" /> 

jquery validate Verification:


jQuery.validator.addMethod("lrunlv", function(value, element) {     
return this.optional(element) || /^\d+(\.\d{1,2})?$/.test(value);     
}, " Decimal places cannot exceed 3 Bit ");  

//Verify the number of digits after the decimal point


jQuery.validator.addMethod("decimals", function(value, element,d){ 
   var a = value.indexOf(".")+1; 
   if(a==0){ 
    a=value.length; 
   } 
   var b = value.length; 
   var c = b-a; 
  return this.optional(element) || c<=d;  
}); 

Related articles: