[英]ng-pattern to check amount should be of 1 to 3 digit with 2 decimal value (999.99)?
I am working on a transaction form. 我正在处理交易表格。 In the form, there is a field name Amount.
在表单中,有一个字段名称Amount。 I am using this ng-pattern ie
ng-pattern="/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(?:\\.[0-9]{1,2})?$/"
which allow only number with 2 decimal value. 我正在使用此ng模式,即
ng-pattern="/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(?:\\.[0-9]{1,2})?$/"
,只允许带两位小数的数字。 Also, 0 is also not eligible for this input pattern. 同样,0也不适用于此输入模式。
<input type="text" ng-model="copayAmount" ng-pattern="/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(?:\.[0-9]{1,2})?$/" maxlength="9" required>
Above expression allow 9 digit value with 2 digit decimal value. 以上表达式允许9位值和2位十进制值。 But I want 3 digit only with 2 decimal value.
但是我只需要3位数字和2个十进制值。
I want ng-pattern for only digit should be valid within only 3 digit (not more than 3 digit) with 2 decimal value ie 999.99 (0 should not be valid). 我希望只有位的ng-pattern应该在仅3位(不超过3位)内有效,并带有2个十进制值,即999.99(0无效)。
Please help. 请帮忙。 Thanks.
谢谢。
Your pattern is working, you can check from https://regex101.com/r/XaumOY/1 您的模式有效,您可以从https://regex101.com/r/XaumOY/1检查
$scope.numberPattern = (function() {
var regexp = /^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(?:\.[0-9]{1,2})?$/;
return {
test: function(value) {
if( $scope.checkedumber=== false ) {
return true;
}
return regexp.test(value);
}
};
})();
And in HTML no changes would be required: 在HTML中,无需进行任何更改:
<input type="text" ng-model="..." ng-required="checkedumber"
ng-pattern="numberPattern" />
Guys I have implement the pattern for this case : 伙计们,我已经实现了这种情况的模式:
ng-pattern="/^[1-9][0-9]{0,2}(\d{3}[-.]?:,?[0-9]{3}){0,3}(?:\.[0-9]{1,2})?$/"
This pattern allow digit more than 0 with 1 to 3 digit & 2 decimal value. 此模式允许1到3位数和2个十进制值的数字大于0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.