[英]Javascript regExp to accept 12 digits before decimal and two digits after decimal
Can anyone tell me the javascript regEx to accept 12 numbers before decimal and to allow only 2 digits after decimal.In case the number does not have any decimal, the text box should accept max of 12 numbers.The value should be only numbers. 任何人都可以告诉我javascript regEx接受十进制前的12个数字,并允许十进制后的仅2个数字。如果该数字不包含任何十进制,则文本框最多应接受12个数字。该值只能是数字。 I already have a directive where i am performing all the javascript validations.Please let me know what else should be added. 我已经有一条指令要执行所有的javascript验证。请让我知道还应该添加什么。
The directive- 该指令-
.directive('allowOnlyDigits', function () {
return {
require: '?ngModel',
scope: { currencycode: '=' },
link: function(scope, element, attrs, ngModelCtrl) {
if(!ngModelCtrl) {
return;
}
ngModelCtrl.$parsers.push(function(val) {
if (angular.isUndefined(val)) {
var val = '';
}
var decimalCheck = clean.split('.');
if(!angular.isUndefined(decimalCheck[1])) {
decimalCheck[1] = decimalCheck[1].slice(0,2);
clean = decimalCheck[0] + '.' + decimalCheck[1];
}
if (val !== clean) {
ngModelCtrl.$setViewValue(clean);
ngModelCtrl.$render();
}
return clean;
});
element.bind('keypress', function(event) {
if(event.keyCode === 32) {
event.preventDefault();
}
});
}
};
})
This is the RegEx you are looking for: /^\\d{1,12}(\\.\\d{1,2})?$/
这是您要查找的RegEx:/ /^\\d{1,12}(\\.\\d{1,2})?$/
Demo (try it yourself): 演示(自己尝试):
var input = document.getElementsByTagName("input")[0] input.oninput = function() { document.getElementById("output").textContent = /^\\d{1,12}(\\.\\d{1,2})?$/.test(input.value) }
<input type="text" /> <div id="output"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.