问题
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 6 years ago.
I want to force my html input the following display: 14 mantissa maximun(from 1 to 14) and the Effective Three decimal point, if the user does not specify the value of the points after they take the default 000
Example: Enter 5 show 5.000
enter 5.2 Show 5.200
enter 22222 show 22222.000 display.
please help
回答1:
Use .indexOf to check for the . character. Make sure it's there and check the length of the substring that goes after its index. If it's less then 3, add some zeros.
HTML
JavaScript
/* Select the desired input, and add an onChange event listener */
document.querySelector('#input').onchange = function () {
/* if the user didn't add a dot, we add one with 3 zeros */
if (this.value.indexOf('.') == -1) this.value += '.000';
/* Otherwise, we check how many numbers there are after the dot
and make sure there's at least 3*/
if (this.value.substring(this.value.indexOf('.') + 1).length < 3)
while (this.value.substring(this.value.indexOf('.') + 1).length < 3)
this.value += '0';
};
Live Demo
回答2:
check if the number match (\d{1,14})(\.\d{1,3}){0,1}
split by .(dot) char
check the length of the second array result
add .000 if null or ajust if have 1 or 2 length by add 2 or 1 zero
来源:https://stackoverflow.com/questions/18273209/force-decimals-in-html-inputs-with-inputmask-html-jqueryl-or-js