AngularJS Form Validation using ngMessages Example

Today our leading topic is add form validation with ng-messages. So you can simply use with registration form, login form, contact form etc of angular. we will use required, minlength, maxlength, email, password etc validation on form submit by button clicking in angular js example.

we will create form with name, email and password fields, make simple realtime validation using angular js. we will display instant error message. we also use bootstrap for design and make it better layout.

I write full example of angular js form validation for beginner. So, let's check bellow example code and do it.


<!DOCTYPE html>



<title>AngularJS Form Validation using ngMessages Example - HDTuto.com</title>

<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" />

<script src="https://code.angularjs.org/1.4.0/angular.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular-messages.js"></script>


<body ng-app="hdTutoApp" ng-controller="HDTutoController as hdTuto">

<div class="container" style="width:600px">

<div class="page-header">

<h3>AngularJS Form Validation using ngMessages Example - HDTuto.com</h3>


<form name="myForm" novalidate>

<div class="form-group" ng-class="{ 'has-error' :

myForm.myName.$touched && myForm.myName.$invalid }">


<input type="text" name="myName" class="form-control"

ng-model="hdTuto.name" ng-minlength="3" ng-maxlength="8" required>

<div class="help-block" ng-messages="myForm.myName.$error"


<p ng-message="required">Your name is required.</p>

<p ng-message="minlength">Your name is too short.</p>

<p ng-message="maxlength">Your name is too long.</p>



<div class="form-group" ng-class="{ 'has-error' :

myForm.userName.$touched && myForm.userName.$invalid }">


<input type="email" name="userName" class="form-control"

ng-model="hdTuto.userName" required>

<div class="help-block" ng-messages="myForm.userName.$error"


<p ng-message="required">This field is required</p>

<p ng-message="email">Please enter a valid email.</p>



<div class="form-group" ng-class="{ 'has-error' :

myForm.password.$touched && myForm.password.$invalid }">


<input type="password" name="password" class="form-control"

ng-model="hdTuto.password" ng-minlength="6" ng-maxlength="8"


<div class="help-block" ng-messages="myForm.password.$error"


<p ng-message="required">This field is required</p>

<p ng-message="minlength">This field is too short</p>

<p ng-message="maxlength">This field is too long</p>

<p ng-message="email">This needs to be a valid email</p>

<p ng-message="password">This needs to be a valid password</p>



<button type="submit" class="btn btn-primary"






<script type="text/javascript">

var hdTutoApp = angular.module('hdTutoApp', ['ngMessages']);

hdTutoApp.controller('HDTutoController', function($scope) {





I hope you found your best...

February 21, 2019 | Category : BootstrapAngular JS
Related Posts

Blog Search