正则表达式--限制input输⼊0~1之间的⼩数,含0,1,最多两位
⼩数
⼀个简单的正则表达式,⼯作中可能经常⽤到,限制字符串的输⼊、数字的输⼊,这些⽤⼀个onkeyup就解决了,如:
οnkeyup="place(/[^\d]/g,'') //限制输⼊数字
input框禁止输入最近有个⼩需求,限制输⼊框输⼊费率,要求:只能输⼊0-1之间的⼩数,最⼤是1,最⼩为0,且最多输⼊2位⼩数
正则表达式:angular中定义⼀个ng-keyup="restrictInp()",写法如下,这样就可以限制了,input属性中加了限制maxlength=4,这样的话能保证最多输⼊2位⼩数
$strictInp = function(input) {
var reg = /^(0.\d+|0|1)$/;
if (st(input)) {
$figValue = input;
} else {
if (input != "0.")
$figValue = "";
}
};
之前把正则表达式放到input输⼊框中直接限制,⼀直有问题:如下(注意:该⽅法不可⾏)
<input type="text" onkeyup="place(/^(0.\d+|0|1)$/,'')" maxlength="4"
ng-model="figValue" />
但平时限制只输⼊数字的时候还是可以直接写在输⼊框的:
<input type="text" onkeyup="place(/[^\d]/g,'')" maxlength="4"
ng-model="figValue" />
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论