當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


AngularJS ng-pattern用法及代碼示例

AngularJS中的ng-pattern指令用於將模式(正則表達式模式)驗證器添加到輸入HTML元素上的ngModel中。如果輸入字段數據與通過評估ngpattern屬性值中指定的Angular表達式找到的RegExp不匹配,則用於設置模式驗證錯誤鍵。

用法:

<element ng-pattern="expression"> Contents... </element>

範例1:本示例使用ng-pattern指令檢查密碼模式。


<!DOCTYPE html> 
<html> 
  
<head> 
    <title>ng-pattern Directive</title> 
      
    <script src= 
"https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"> 
    </script>  
      
    <style> 
        .red { 
            color:red 
        } 
        .green { 
            color:green 
        } 
    </style> 
</head> 
  
<body ng-app="app" style="text-align:center"> 
      
    <h1 style="color:green;">GeeksforGeeks</h1> 
    <h2 style="">ng-pattern Directive</h2> 
  
    <div ng-controller="geek"> 
        <ng-form name="pass"> 
            Password:<input type="password" ng-model="password"
                name="password" ng-pattern="re" /><br> 
                  
            <span ng-show="pass.password.$error.pattern" 
                    style="color:red"> 
                Not valid password. 
            </span><br> 
              
            Confirm:<input type="password" ng-model="repass"
                    ng-keyup="compare(repass)" name="repass"
                    ng-pattern="re" /><br> 
              
            <span ng-show="isconfirm || pass.repass.$dirty "
                ng-class="{true:'green',false:'red'}[isconfirm]"> 
                Password {{isconfirm==true?'':'not'}} match 
            </span> 
        </ng-form> 
    </div> 
      
    <script> 
        var app = angular.module("app", []); 
        app.controller('geek', ['$scope', function ($scope) { 
            $scope.re = /^[a-zA-Z]\w{3,14}$/; 
              
            $scope.compare = function (repass) { 
                $scope.isconfirm = $scope.password == repass ? 
                        true:false; 
            } 
        }]); 
    </script> 
</body> 
  
</html>                    

輸出:

  • 輸入無效:
    ngpattern
  • 輸入不匹配:
    ngpattern
  • 有效輸入:
    ngpattern

範例2:如果輸入不是數字,則此示例顯示錯誤。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title>ng-pattern Directive</title> 
      
    <script src= 
"https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"> 
    </script>  
</head> 
  
<body ng-app="app" style="text-align:center"> 
      
    <h1 style="color:green;">GeeksforGeeks</h1> 
    <h2 style="">ng-pattern Directive</h2> 
  
    <div ng-controller="geek"> 
        <ng-form name="num"> 
            Input Number:<input type="text" ng-model="number"
                name="number" ng-pattern="re" /><br /> 
              
            <span ng-show="num.number.$error.pattern" style="color:red"> 
                Input is not valid. 
            </span> 
        </ng-form> 
    </div> 
      
    <script> 
        var app = angular.module("app", []); 
          
        app.controller('geek', ['$scope', function ($scope) { 
            $scope.re = /^[0-9]{1,6}$/; 
        }]); 
    </script> 
</body> 
  
</html>                    

輸出:

  • 輸入為文字:
    ngpattern
  • 輸入的是數字:
    ngpattern


相關用法


注:本文由純淨天空篩選整理自Vishal Chaudhary 2大神的英文原創作品 AngularJS | ng-pattern Directive。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。