How to use js to limit text boxes to only numbers and decimal points, and only two decimal places?

  javascript

First place cannot beZeroorFull stop
You cannot have two at the same time.Full stop
The last one cannot beFull stop
I can just type20You can also enter20.25You can also enter only20.2
Is there a good writing style?


$("#amount").on('keyup', function (event) {
 var $amountInput = $(this);
 //In response to mouse events, the left and right direction keys are allowed to move.
 event = window.event || event;
 if (event.keyCode == 37 | event.keyCode == 39) {
 return;
 bracket
 //First, replace all non-numeric items except the numbers and.
 $amountInput.val($amountInput.val().replace(/[^\d.]/g, "").
 //Only one decimal point is allowed
 replace(/^\./g, "").replace(/\.{2,}/g, ".").
 //Only two decimal places can be entered
 Replace (".","$ # $"). replace (\./g," "). replace ("$ # $ ",""). replace ((\-) * (\ d plus) \.(\d\d).*$/, '$1$2.$3');
 });
 $("#amount").on('blur', function () {
 var $amountInput = $(this);
 //If the last digit is decimal point, remove it
 $amountInput.val(($amountInput.val().replace(/\.$/g, "")));
 });

It was only necessary to enter a positive number, which was used before.isNANIt can be done. In the morning, I temporarily changed my demand, searched Miss. Du and used the above judgment. I always felt that I should not use so much judgment, so I asked everyone here.

After studying it myself, I feel it is ok to write like this? Replacing what the user inputs, who knows what strange character strings the user will input … I am also a novice, write poorly, learn from each other … XD

$(document).on('keypress', '#id', function (e) {
 //Intercept incorrect input in keypress event
 
 var sCharCode = String.fromCharCode(e.charCode);
 var sValue = this.value;
 
 if (/[^0-9.]/g.test(sCharCode) || __getRegex(sCharCode).test(sValue)) {
 return false;
 bracket
 
 /**
 * according to the user input characters to obtain relevant regular expressions
 * @param {string} sCharCode user-entered characters, such as' a',' 1',' etc.
 * @return {regexp} patt regular expression
 */
 function __getRegex (sCharCode) {
 var patt;
 if (/[0]/g.test(sCharCode)) {
 //Judge whether it is empty
 patt = /^$/g;
 } else if (/[.]/g.test(sCharCode)) {
 //Judge whether it already contains. characters or is empty
 patt = /((\.)|(^$))/g;
 } else if (/[1-9]/g.test(sCharCode)) {
 //Judge whether two decimal places have been reached
 patt = /\.\d{2}$/g;
 bracket
 return patt;
 bracket
 }).on('keyup paste', '#id', function () {
 //complete string detection in keyup paste event
 
 var patt = /^((?  !  0)\d plus (\.\d{1,2})?  )$/g;
 
 if (!  patt.test(this.value)) {
 //Error prompt related codes, red border, bubble prompt, etc
 Log ('incorrect input format!'  );
 bracket
 });

= = = = = = = = = = = = = = = = = = = = = = = = = = = = dividing line = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
^((? ! 0)\d plus (.\d{1,2})? )$

Indicates the beginning.
 (?  !  0) cannot start with 0
 \d plus integer part
 (\.\d{1,2})?  Decimal part, where (\.\d{1,2}) means two decimal places,?  Indicates zero or one match
 $ means end

test
图片描述

Generally speaking, if the input is wrong, an error prompt will pop up or the edit box will turn red. Is it really good to modify the user’s input directly?