簡體   English   中英

ng模式檢查金額應為1到3位數字,並帶有2個十進制值(999.99)?

[英]ng-pattern to check amount should be of 1 to 3 digit with 2 decimal value (999.99)?

我正在處理交易表格。 在表單中,有一個字段名稱Amount。 我正在使用此ng模式,即ng-pattern="/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(?:\\.[0-9]{1,2})?$/" ,只允許帶兩位小數的數字。 同樣,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> 

以上表達式允許9位值和2位十進制值。 但是我只需要3位數字和2個十進制值。

我希望只有位的ng-pattern應該在僅3位(不超過3位)內有效,並帶有2個十進制值,即999.99(0無效)。

請幫忙。 謝謝。

您的模式有效,您可以從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);
        }
    };
})();

在HTML中,無需進行任何更改:

<input type="text" ng-model="..." ng-required="checkedumber"
    ng-pattern="numberPattern" />

伙計們,我已經實現了這種情況的模式:

ng-pattern="/^[1-9][0-9]{0,2}(\d{3}[-.]?:,?[0-9]{3}){0,3}(?:\.[0-9]{1,2})?$/"

此模式允許1到3位數和2個十進制值的數字大於0。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM