首页 > 代码库 > js输入控制(只能输入整数小数等)

js输入控制(只能输入整数小数等)

1.页面

<asp:TextBox runat="server" ID="NumZYZCZMJ" OnKeyUp="InputFloat(this)"></asp:TextBox>

2.JS

 //只能输入整数
        function InputNumber(e) {
            e.value = http://www.mamicode.com/e.value.replace(/D/g,"");
            if (e.value.substring(0, 1) == "0") {
                e.valuehttp://www.mamicode.com/= "http://www.mamicode.com/0";
            }

 //只能输入浮点数         function InputFloat(e) {

            e.value = http://www.mamicode.com/e.value.replace(/[^/d]/g,"");       

      if (e.value.substring(0, 2) == "00") {      

           e.valuehttp://www.mamicode.com/= "http://www.mamicode.com/0";       

     }         

    if (e.value.substring(0, 1) == ".")

{                 e.valuehttp://www.mamicode.com/= "";             }        

     if (e.value.split(‘.‘).length == 2) {        

         e.value = http://www.mamicode.com/e.value.split(‘.‘)[0] + ‘.‘ + e.value.split(‘.‘)[1].substring(0, 2); }

      if (e.value.split(‘.‘).length > 2) {                 e.value = http://www.mamicode.com/e.value.split(‘.‘)[0] + ‘.‘ + e.value.split(‘.‘)[1]; }

js输入控制(只能输入整数小数等)