AngularJS Form Validation
Submitted by nurhodelta_17 on Friday, January 12, 2018 - 19:08.
Getting Started
I've used CDN for Bootstrap and Angular JS in this tutorial so you need internet connection for them to work.Form and Input State
This our the states in Angular JS.- $untouched The field has not been touched yet
- $touched The field has been touched
- $pristine The field has not been modified yet
- $dirty The field has been modified
- $invalid The field content is not valid
- $valid The field content is valid
Validation Rules used in this tutorial
- email - The input should in email format
- url - The input should be a url
- required - The input field should not be empty
- ng-minlength - Input must be greater than or equal to the set value
- ng-maxlength - Input must be less than or equal to the set value
- ng-pattern - The input should look like the set pattern (in this case of this tutorial, the input should follow to set REGEX format)
index.html
This is our index that contains our form and validations.- <!DOCTYPE html>
- <html lang="en" ng-app="app">
- <head>
- <meta charset="utf-8">
- <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
- <style type="text/css">
- .errortext{
- color:red;
- }
- </style>
- </head>
- <body ng-controller="myController">
- <div class="container">
- <div class="col-md-6 col-md-offset-3">
- <div class="login-panel panel panel-primary">
- <div class="panel-heading">
- </div>
- <div class="panel-body">
- <form role="form" name="myForm" novalidate>
- <fieldset>
- <div class="form-group">
- <input type="text" class="form-control" name="username" placeholder="Username" ng-minlength="10" ng-maxlength="30" ng-pattern="/^[a-zA-Z0-9_]*$/" ng-model="user.username" required autofocus>
- <div class="errortext" ng-show="myForm.username.$dirty && myForm.username.$invalid">
- </div>
- </div>
- <div class="form-group">
- <input type="password" class="form-control" name="password" placeholder="Password" ng-model="user.password" required>
- <div class="errortext" ng-show="myForm.password.$dirty && myForm.password.$invalid">
- </div>
- </div>
- <div class="form-group">
- <input type="password" class="form-control" name="repassword" placeholder="Re-type Password" ng-model="user.repassword" required>
- <div class="errortext" ng-show="myForm.repassword.$dirty && myForm.repassword.$invalid || myForm.repassword.$dirty && user.repassword != user.password">
- </div>
- </div>
- <div class="form-group">
- <input type="email" class="form-control" name="email" placeholder="Email" ng-model="user.email" required>
- <div class="errortext" ng-show="myForm.email.$dirty && myForm.email.$invalid">
- </div>
- </div>
- <div class="form-group">
- <input type="url" class="form-control" name="url" placeholder="Website" ng-model="user.website" required>
- <div class="errortext" ng-show="myForm.url.$dirty && myForm.url.$invalid">
- </div>
- </div>
- <div class="form-group">
- <input type="text" class="form-control" name="firstname" placeholder="Firstname" ng-model="user.firstname" required>
- <div class="errortext" ng-show="myForm.firstname.$dirty && myForm.firstname.$invalid">
- </div>
- </div>
- <div class="form-group">
- <input type="text" class="form-control" name="lastname" placeholder="Lastname" ng-model="user.lastname" required>
- <div class="errortext" ng-show="myForm.lastname.$dirty && myForm.lastname.$invalid">
- </div>
- </div>
- </fieldset>
- </form>
- </div>
- </div>
- <div class="alert alert-success text-center" ng-show="valid">
- </div>
- </div>
- </div>
- </body>
- </html>
angular.js
This contains our angular js scripts.- var app = angular.module('app', []);
- app.controller('myController', function($scope){
- $scope.valid = false;
- $scope.submit = function(){
- $scope.valid = true;
- }
- $scope.close = function(){
- $scope.valid = false;
- }
- });
Add new comment
- 164 views