简体   繁体   English

JavaScript中的正则表达式,仅允许带有可选2位小数的数字

[英]Regular Expression in JavaScript to allow only numbers with optional 2 decimals

I want to write a regular expression in which allows 我想写一个允许的正则表达式

  1. backspace 退格
  2. 0-9 digits 0-9位数
  3. optional fractional part with two decimals (no limit on integral part how many digits there can be) 带有两位小数的可选小数部分(对于整数部分没有限制可以有多少位数)

For example: 例如:

  • Allowed lists is [12 , 232.0 , 23.(with only dot) , 345.09 , 78.23 , 134.00 , 0.21 , .21 , .02 , .01 .12 ] 允许列表为[12,232.0,23。(仅带点),345.09,78.23,134.00,0.21,.21,.02,.01 .12]
  • Not allowed are [12.878 , 34.343.334 , .0003 ] 不允许[12.878,34.343.334,.0003]

The use of this regular expression would be like on javascript event 这个正则表达式的使用就像在javascript事件上一样

<input type="text"  onKeyPress="validatenumber(event);"   /><br>

My code is 我的代码是

function validatenumber(evt) {
  var theEvent = evt || window.event;
  var key = theEvent.keyCode || theEvent.which;
  key = String.fromCharCode( key );
  var regex = /^[0-9\b]+$/;    // allow only numbers [0-9] 
  if( !regex.test(key) ) {
    theEvent.returnValue = false;
    if(theEvent.preventDefault) theEvent.preventDefault();
  }
}

I want to change only this line with the new regex: 我想用新的正则表达式只改变这一行:

var regex = /^[0-9\b]+$/;    // allow only numbers [0-9]

Here it is: 这里是:

/^\d*(?:\.\d{1,2})?$/

Working Demo: http://jsfiddle.net/qd7BL/ 工作演示: http//jsfiddle.net/qd7BL/

find the final solution at least 至少找到最终解决方案

<input id="txtId" type="text" onkeyup="NumAndTwoDecimals(event , this);"></input>

and the

 function NumAndTwoDecimals(e , field) {
    var val = field.value;
    var re = /^([0-9]+[\.]?[0-9]?[0-9]?|[0-9]+)$/g;
    var re1 = /^([0-9]+[\.]?[0-9]?[0-9]?|[0-9]+)/g;
    if (re.test(val)) {
        //do something here

    } else {
        val = re1.exec(val);
        if (val) {
            field.value = val[0];
        } else {
            field.value = "";
        }
    }
}

这个正则表达怎么样:

^\d*(?:\.\d\d)?$

This below simple html code will validate the required value and also +ve & -ve numbers of atleast 1 digit additionally optional 2 digits after decimal point. 以下简单的html代码将验证所需的值以及至少1位数的+ ve和-ve数,另外还可选小数点后2位数。

JS Code: JS代码:

        function validateNum() {

            /*
            For mandatry input field, use variable: "patForReqdFld".
            For optional input filed, use variable: "patForOptFld".
            */

            var patForReqdFld = /^(\-)?([\d]+(?:\.\d{1,2})?)$/;
            var patForOptFld = /^(\-)?([\d]*(?:\.\d{1,2})?)$/;

            var value = document.getElementById('txtNum').value;

            if(patForReqdFld.test(value)) {
                alert('Valid Number: ' + value);
            } else {
                alert('Invalid Number: ' + value);
            }
        }

HTML Code: HTML代码:

        <label>Enter Number:&nbsp;</label>
        <input type="text" id="txtNum" onBlur="validateNum()"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM