使用InputMask html(jqueryl或js)强制html输入中的小数? [英] Force decimals in html inputs with InputMask html (jqueryl or js)?

查看:618
本文介绍了使用InputMask html(jqueryl或js)强制html输入中的小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想强制我的html输入如下显示:14个尾数maximun(从1到14)和有效三位小数点,如果用户在默认000 $ b $后没有指定点的值b示例:输入5显示5.000
输入5.2显示5.200
输入22222显示22222.000显示。
请帮助

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

推荐答案

使用 .indexOf 来检查字符。确保它在那里,并检查其索引后面的 substring 的长度。

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.

<input type="text" id="input" maxlength="14" value="" />



JavaScript



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';
};

这篇关于使用InputMask html(jqueryl或js)强制html输入中的小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆