This code provides an efficient way to implement form validation for login forms in AngularJS. With this code, you can ensure that users provide accurate and complete information before submitting the form. The code creates an Angular app called “validationApp” and a corresponding controller named “mainController”. Inside the controller, the submitForm
function is triggered when the form is submitted, performing validation checks to ensure all required fields are correctly filled out.
Whether you’re building a login page for a website or an authentication system, this code snippet will help you streamline the form submission process.
How to Validate Login Form on Submit using AngularJS
First of all, load the following assets into the head tag of your HTML document.
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" />
Create the HTML structure as follows:
<div ng-app="validationApp" ng-controller="mainController"> <div class="container"> <!-- PAGE HEADER --> <div class="page-header"><h1>AngularJS Form Validation</h1></div> <!-- =================================================================== --> <!-- FORM ============================================================== --> <!-- =================================================================== --> <!-- pass in the variable if our form is valid or invalid --> <form class="login_form" name="userForm" ng-submit="submitForm(userForm.$valid)" novalidate> <!-- NAME --> <div class="form-group" ng-class="{ 'has-error' : userForm.name.$invalid && !userForm.name.$pristine }"> <label>Name*</label> <input type="text" name="name" class="form-control" ng-model="user.name" required> <p ng-show="userForm.name.$invalid && !userForm.name.$pristine" class="help-block">You name is required.</p> </div> <!-- USERNAME --> <div class="form-group" ng-class="{ 'has-error' : userForm.username.$invalid && !userForm.username.$pristine }"> <label>Username</label> <input type="text" name="username" class="form-control" ng-model="user.username" ng-minlength="3" ng-maxlength="8"> <p ng-show="userForm.username.$error.minlength" class="help-block">Username is too short.</p> <p ng-show="userForm.username.$error.maxlength" class="help-block">Username is too long.</p> </div> <!-- EMAIL --> <div class="form-group" ng-class="{ 'has-error' : userForm.email.$invalid && !userForm.email.$pristine }"> <label>Email</label> <input type="email" name="email" class="form-control" ng-model="user.email"> <p ng-show="userForm.email.$invalid && !userForm.email.$pristine" class="help-block">Enter a valid email.</p> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> <!-- =================================================================== --> <!-- VALIDATION TABLES ================================================= --> <!-- =================================================================== --> <div class="page-header"><h1>Validation Tables</h1></div> <div class="row"> <div class="col-xs-3"> <h3>Form</h3> <table class="table table-bordered"> <tbody> <tr> <td ng-class="{ success: userForm.$valid, danger: userForm.$invalid }">Valid</td> </tr> <tr> <td ng-class="{ success: userForm.$pristine, danger: !userForm.$pristine }">Pristine</td> </tr> <tr> <td ng-class="{ success: userForm.$dirty }">Dirty</td> </tr> </tbody> </table> </div> <div class="col-xs-3"> <h3>Name</h3> <table class="table table-bordered"> <tbody> <tr> <td ng-class="{ success: userForm.name.$valid, danger: userForm.name.$invalid }">Valid</td> </tr> <tr> <td ng-class="{ success: userForm.name.$pristine, danger: !userForm.name.$pristine }">Pristine</td> </tr> <tr> <td ng-class="{ success: userForm.name.$dirty }">Dirty</td> </tr> </tbody> </table> </div> <div class="col-xs-3"> <h3>Username</h3> <table class="table table-bordered"> <tbody> <tr> <td ng-class="{ success: userForm.username.$valid, danger: userForm.username.$invalid }">Valid</td> </tr> <tr> <td ng-class="{ success: userForm.username.$pristine, danger: !userForm.username.$pristine }">Pristine</td> </tr> <tr> <td ng-class="{ success: userForm.username.$dirty }">Dirty</td> </tr> </tbody> </table> </div> <div class="col-xs-3"> <h3>Email</h3> <table class="table table-bordered"> <tbody> <tr> <td ng-class="{ success: userForm.email.$valid, danger: userForm.email.$invalid }">Valid</td> </tr> <tr> <td ng-class="{ success: userForm.email.$pristine, danger: !userForm.email.$pristine }">Pristine</td> </tr> <tr> <td ng-class="{ success: userForm.email.$dirty }">Dirty</td> </tr> </tbody> </table> </div> </div> </div> </div>
Style using the following CSS styles:
.login_form{ max-width: 640px; margin: 10px auto; padding: 12px; box-shadow: 1px 2px 8px rgba(0, 0, 0, 0.30); }
Load the AngularJS by adding the following CDN link before closing the body tag:
<script src="https://code.angularjs.org/1.2.6/angular.js"></script>
Finally, add the following JavaScript function to validate login form.
// create angular app var validationApp = angular.module('validationApp', []); // create angular controller validationApp.controller('mainController', function($scope) { // function to submit the form after all validation has occurred $scope.submitForm = function(isValid) { // check to make sure the form is completely valid if (isValid) { alert('our form is amazing'); } }; });
That’s all! hopefully, you have successfully integrated this AngularJS login form with validation into your project. If you have any questions or suggestions, feel free to comment below.