The
ng-pattern is used to validate the input field with the help of regular
expressions. Its return true if field is validate as per expressions
otherwise return false.
The AngularJs code-sample
var app = angular.module("myApp", []); app.controller("myCtrl", function ($scope) { $scope.IsValid = function () { alert("Price Is valid and form is submitted."); } });
The HTML code-sample
<div ng-app="myApp"> <div ng-controller="formCtrl"> <form name="valForm" ng-submit="IsValid()"> <input type="number" ng-model="val" name="val" ng-pattern="/^\d{0,9}(\.\d{1,9})?$/" required> <input type="submit" value="submit" /> </form> </div> </div>
The Live demo code-sample
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script> <script> var app = angular.module("myApp", []); app.controller("myCtrl", function ($scope) { $scope.IsValid = function () { alert("Price Is valid and form is submitted."); } }); </script> </head> <body ng-app="myApp"> <div ng-controller="formCtrl"> <form name="valForm" ng-submit="IsValid()"> <input type="number" ng-model="val" name="val" ng-pattern="/^\d{0,9}(\.\d{1,9})?$/" required> <input type="submit" value="submit" /> </form> </div> </body> </html>
The output:
Blogger Comment
Facebook Comment