This time I will bring you JS verification input to retain specified decimals, and JS validation input to retain specified decimals.What are the precautions?. Here is a practical case, let's take a look.
1. Verification method validationNumber(e, num) e represents the label object, num represents the number of decimal places to be retained
function validationNumber(e, num) { var regu = /^[0-9]+\.?[0-9]*$/; if (e.value != "") { if (!regu.test(e.value)) { alert("请输入正确的数字"); e.value = e.value.substring(0, e.value.length - 1); e.focus(); } else { if (num == 0) { if (e.value.indexOf('.') > -1) { e.value = e.value.substring(0, e.value.length - 1); e.focus(); } } if (e.value.indexOf('.') > -1) { if (e.value.split('.')[1].length > num) { e.value = e.value.substring(0, e.value.length - 1); e.focus(); } } } } }
2. Verification integer
3. One digit is reserved Decimal
4. Keep two decimal places
5. Keep three decimal places
Just putvalidationNumber(this,3)
in the method Just change the second parameter to 3.
Keep four decimal places, keep five decimal places, and so on...
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website !
Recommended reading:
vue environment transfer parameters packaging different domain name code analysis
js realizes the mutual transfer of Json code between the front and backend
The above is the detailed content of JS validation input retains specified decimals. For more information, please follow other related articles on the PHP Chinese website!