AngularJS Tutorial - Use ng-pattern to validate








The following code shows how to use ng-pattern to validate.

Example


<!DOCTYPE html>
<html  ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
<script type='text/javascript'>
function myApp($scope) {<!--from   ww w.ja  v a2  s  .  c o  m-->
    $scope.isOdd = function ($value) {
        return $value % 2;
    };
}
</script>
</head>
<body ng-app="ui.validate">
  <div ng-controller="myApp">
    <form name="form">
        <input ng-model="myVal" 
               name="value" 
               required 
               ng-pattern="/^[0-9]*$/" ></input> 
         <pre>Errors: {{form.value.$error|json}}</pre>
    </form>
</div>
</body>

</html>

The code above is rendered as follows: