html input decimal,Force decimals in html inputs with InputMask html (jqueryl or js)? [closed]

问题

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值