Use ng-pattern to validate

Description

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   w  ww.  j  a v a 2s.co  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>

Click to view the demo





















Home »
  AngularJS »
    AngularJS Example »




Controller
Directives
Expression
Filter
Form
Inject
Scope
Server
Style
Template
Utilities