Simple validation through AngularJS directives.
A Pen by Bruno Scopelliti on CodePen.
Simple validation through AngularJS directives.
A Pen by Bruno Scopelliti on CodePen.
| <!doctype html> | |
| <html lang="en" ng-app="myApp"> | |
| <head> | |
| <meta charset="utf-8" /> | |
| <title>My AngularJS App</title> | |
| </head> | |
| <body ng-controller="stageController"> | |
| <form name="myForm" novalidate ng-submit="submitForm();"> | |
| <h3>Basic example</h3> | |
| <div class="form-section"> | |
| <p>Username:</p> | |
| <input type="text" name="username1" ng-model="username1" | |
| required /> | |
| <input type="submit" ng-disabled="myForm.username1.$pristine || myForm.username1.$dirty && myForm.username1.$invalid"> | |
| <div class="custom-error" ng-show="myForm.username1.$dirty && myForm.username1.$invalid">Invalid: | |
| <span ng-show="myForm.username1.$error.required">Username is required.</span> | |
| </div> | |
| <p>Email:</p> | |
| <input type="email" name="email1" ng-model="email1" required | |
| /> | |
| <input type="submit" ng-disabled="myForm.email1.$pristine || myForm.email1.$dirty && myForm.email1.$invalid" | |
| /> | |
| <div class="custom-error" ng-show="myForm.email1.$dirty && myForm.email1.$invalid">Invalid: | |
| <span ng-show="myForm.email1.$error.required">Email is required.</span> | |
| <span | |
| ng-show="myForm.email1.$error.email">Please, write a valid email address.</span> | |
| </div> | |
| </div> | |
| </form> | |
| </body> | |
| </html> |
| 'use strict'; | |
| // Declare app level module which depends on filters, and services | |
| angular.module('myApp', []); | |
| /* Controllers */ | |
| function stageController($scope) { | |
| $scope.username1 = 'Peter Parker'; | |
| $scope.email1 = 'pparker@gmail.com'; | |
| $scope.submitForm = function () { | |
| console.info("Here I should implement the logic to send a request to the server."); | |
| } | |
| } |
| </style> <!-- Ugly Hack due to jsFiddle issue: http://goo.gl/BUfGZ --> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script> <style> .form-section { | |
| font-size:14px; | |
| margin:10px 0; | |
| } | |
| .form-section p { | |
| margin:5px 0; | |
| } | |
| .form-section input:not([type='submit']) { | |
| height:18px; | |
| padding:2px 4px; | |
| width:220px; | |
| } | |
| .form-section .custom-error { | |
| color:#FF0000; | |
| } | |
| [type='submit'] { | |
| background:@EEEEEE; | |
| border:1px solid #666666; | |
| height:26px; | |
| padding:4px 5px 3px; | |
| vertical-align:-1px; | |
| width:50px; | |
| } |