HTML:

<input type="tel" class="capital mui-input-clear" value="0.00">
 
<div class="redbag_box4_son">¥0.00</div>
 

JS:

$(document).ready(function(){
    // 限制文本框只能输入数字和小数点
    var precapital;
    document.querySelector('.capital').addEventListener('focus', function() {
        if (this.value == '0.00') {
            this.value = '';
        } else {
            this.value = this.value.replace(/\.00/, '').replace(/(\.\d)0/,'$1');
        }
        precapital = this.value;
    });
    document.querySelector('.capital').addEventListener('keyup', function() {
        this.value = this.value.replace(/^[\.]/, '').replace(/[^\d.]/g, '');
        if (this.value.split(".").length - 1 > 1) {
            this.value = precapital;
        }
        precapital = this.value;
    });
    document.querySelector('.capital').addEventListener('blur', function() {
        this.value = this.value.replace(/[\.]$/, '');
        this.value = this.value.replace(/(.*)\.([\d]{2})(\d*)/g,'$1.$2');
        this.value = Number(this.value).toFixed(2);
        var logNum = this.value.toString();
        if(logNum.match(/\./g) != null){
            integerNum = parseInt(logNum).toString().replace(/\d(?=(\d{3})+$)/g,'$&,');
            decimalNum = '.' + logNum.replace(/(.*)\.(.*)/g,'$2');
            document.querySelector(".redbag_box4_son").innerHTML = '¥'+integerNum+decimalNum;
        }else{
            document.querySelector(".redbag_box4_son").innerHTML = logNum.replace(/\d(?=(\d{3})+$)/g,'$&,');
        }
    });
})

参考链接:https://www.cnblogs.com/kousuke/p/perfect-input-for-money.html

input框限制输入金额的更多相关文章

  1. js—input框中输入数字,动态生成内容的方法

    项目中需要在前端实现: 用户输入数字n,动态生成n个元素,删除n,自动清空n个元素(如图一): 用户输入数字n,失焦生成n个元素,再聚焦修改n,自动清空n个元素(如图二): 图一: 图二: 需求一实现 ...

  2. jquery限制文本框只能输入金额

    $("#batch_diff_percent").keyup(function () { var reg = $(this).val().match(/\d+\.?\d{0,2}/ ...

  3. JS 限制input框的输入字数,并提示可输入字数

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  4. js控制input框只能输入数字和一位小数点和小数点后面两位小数

    <script language="JavaScript" type="text/javascript"> function clearNoNum( ...

  5. 使用正则限制input框只能输入数字/英文/中文等等

    常用HTML正则表达式 1.只能输入数字和英文的: 复制代码代码如下: <input onkeyup="value=value.replace(/[/W]/g,'') " o ...

  6. input框的输入限制

    1.输入数字 <input onKeyUp="value=value.replace(/[^\d|chun]/g,'')"> 2.只输入中文 <input typ ...

  7. 使用UIkit的uk-form-icon后input框无法输入的问题

    相关版本UIkit2.27.5 uikit.min.css默认使用uk-form-icon的属性pointer-events: none:因此表框无法点击. <style type=text/c ...

  8. javascript判断input框只能输入数字的方法

    javascript 只允许输入数字有很多方法,总结如下 1,只允许输入数字和小数点. <input onKeypress="return (/[\d.]/.test(String.f ...

  9. vue开发中利用正则限制input框的输入(手机号、非0开头的正整数等)

    我们在前端开发中经常会碰到类似手机号输入获取验证码的情况,通常情况下手机号的输入需要只能输入11位的整数数字.并且需要过滤掉一些明显不符合手机号格式的输入,那么我们就需要用户在输入的时候就控制可以输入 ...

随机推荐

  1. NFS数据共享(全面讲解使用教程)

    目录 一:NFS数据共享 1.NFS简介: 2.什么是NFS? 3.NFS的应用 二:NFS数据共享实践 二:NFS配置详解 1.控制文件权限 三:配置文件分类 四:NFS统一用户 1.创建用户(客户 ...

  2. 容器docker网络解析

    如果想要实现两台主机之间相连通信,最直接的办法是找一根网线连起来, 多台的话需要用网线将他们链接再交换机上. linux中能够起到虚拟交换机的网络设备是网桥birdge, 工作再链路层, 主要是根据m ...

  3. React之常用技术栈

     · react-redux:https://www.cnblogs.com/jingxuan-li/p/12439181.html  · react-router-dom:https://www.c ...

  4. jsp 4-14 知识总结

    二   string类型 装换 boolean 的方法? 三   attribute对象  set 和 get  的用法 四  jsp 的四种属性范围? 五  jsp  <%  %>  和 ...

  5. aws vpc 知识总结(助理级)

    一 什么是vpc? Amazon Virtual Private Cloud(Amazon VPC)使您可以将AWS资源启动到您定义的虚拟网络中. 虚拟的云计算. /* 1 默认vpc ? 创建一个具 ...

  6. bom案例5-简单动画

      <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&q ...

  7. Docker入门 安装 基础操作命令

    Docker 学习来源 https://www.bilibili.com/video/av26993050/?spm_id_from=333.788.b_636f6d6d656e74.20 https ...

  8. 淘大大出了composer镜像 -- 给力

    composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

  9. Python—高级函数

    Python-高级函数 一.闭包 Python函数是支持嵌套的.如果在一个内部函数中对外部函数作用域(非全局作用域)的变量进行引用,那么内部函数就会被称为闭包.闭包需要满足如下3个条件: 存在于两个嵌 ...

  10. 任意文件上传漏洞syr

    任意文件上传漏洞 先来几个一句话木马看看 <%execute(request("value"))%> #asp木马 <?php eval($_POST[" ...