Created
February 18, 2016 18:36
-
-
Save sancau/e90144b28e5e42da893d to your computer and use it in GitHub Desktop.
AngularJS | Simple Validation Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>AngularJS Validation</title> | |
<!-- Angular 1.4.9 CDN --> | |
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js"></script> | |
<style> | |
.valid { | |
background-color: green; | |
} | |
.invalid { | |
background-color: red; | |
} | |
</style> | |
<script> | |
angular.module('app', []) | |
.controller('Ctrl', function($scope) { | |
$scope.onSubmit = function(){ | |
$scope.theForm.$pristine = false; | |
if ($scope.theForm.$valid) { | |
alert('Form is valid!'); | |
} | |
}; | |
}); | |
</script> | |
</head> | |
<body ng-app="app" ng-controller="Ctrl"> | |
<h2>Simple AngularJS Validation Example</h2> | |
<form | |
novalidate | |
role="form" | |
name="theForm" | |
ng-model="formModel"> | |
Some required field: | |
<input type="text" | |
name="text" | |
ng-model="formModel.text" | |
required> | |
<button type="submit" ng-click="onSubmit()"> | |
Submit | |
</button> | |
<div class="invalid" | |
ng-hide="theForm.$valid || theForm.$pristine"> | |
Field is required | |
</div> | |
<div class="valid" ng-hide="!theForm.$valid"> | |
Input is valid | |
</div> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment