AngularJS
What is $Scope in AngularJS? Tutorial with Example
What is $scope in AngularJS? $scope in AngularJS is a built-in object which basically binds the...
Validation is the process ensuring that data is correct and complete.
In a real-world example, let's assume a site which requires a registration form to be completed before getting full access to this site. The registration page would have input fields for username, password, email id and so forth.
When the user submits the form, normally a validation would occur first before the details are sent to the server. This validation would try to ensure to the best possible extent that the details for the input fields are entered in the right manner.
For example, the email id always needs to be in a format of This email address is being protected from spambots. You need JavaScript enabled to view it.; if someone enters just the username in the email id, then ideally the validation should fail. So validation looks at doing these basic checks before the details are sent to the server for further processing.
In this tutorial, you will learn-
Form validation is the process of pre-validating information entered on a web form by the user before it is sent to the server. It's always better to validate the information on the client side itself. This is because it adds less overhead if the user had to be presented with the form again if the information entered was wrong.
Let's have a look at how form validation can be conducted in HTML5.
In our example, we will show one simple registration form to the user in which the user needs to enter details such as a username, password, email id, and age.
The form will have validation controls to ensure that the user enters the information in a proper manner.
<!DOCTYPE html>
<html>
<head>
<meta chrset="UTF 8">
<title>Event Registration</title>
</head>
<body ng-app="sampleApp">
<script src="https://code.angularjs.org/1.6.9/angular-route.js"></script>
<script src="https://code.angularjs.org/1.6.9/angular.min.js"></script>
<script src="https://code.angularjs.org/1.6.9/angular.js"></script>
<script src="/lib/bootstrap.js"></script>
<script src="/lib/bootstrap.css"></script>
<h1> gtupapers Global Event</h1>
<div ng-controller="AngularController">
<form>
Enter your user name:
<input type="text" name="name" required><br><br>
Enter your password:
<input type="password"/><br><br>
Enter your email:
<input type="email"/><br><br>
Enter your age:
<input type="number" /><br><br>
<input type="submit" value="Submit"/>
</form>
</div>
</body>
</html>
Code Explanation:
If the code is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
To see the form validation in action, click the Submit button without entering any information on the screen.
After the submit button is clicked, a pop-up will come showing a validation error that the field needs to be filled.
So the validation for the control which was marked as required, causes the error message to be shown if the user does not enter any value in the text field.
When the user enters any value in the password control, you will notice the '*' symbol used to mask the characters being entered.
Let's then enter wrong email id and click the submit button. After the submit button is clicked, a pop-up will appear showing a validation error that the field needs to have the @ symbol.
So the validation for the control which was marked as an email control will cause the error message to be shown if the user does not enter a proper email id in the text field.
Finally, when you try to enter any characters in the age text control, it will not be entered on the screen. The control will only populate with a value when a number is entered in the control.
AngularJS provides its additional properties for validation. AngularJS provides the following properties for controls for validation purposes
Below are the steps which need to be followed to carry out Angular validation.
Step 1) Use the no validate property when declaring the form. This property tells HTML5 that the validation would be done by AngularJS.
Step 2) Ensure that the form has a name defined for it. The reason for doing this is that, when carrying out Angular validation, the form name will be used.
Step 3) Ensure each control also has a name defined for it. The reason for doing this is that, when carrying out Angular validation, the control name will be used.
Step 4) Use the ng-show directive to check for the $dirty, $invalid and $valid properties for the controls.
Let's look at an example, which incorporates the above-mentioned steps.
In our example,
We are just going to have a simple text field in which the user needs to enter a Topic name in the text box. If this is not done, a validation error will be triggered, and the error message will be shown to the user.
<!DOCTYPE html>
<html>
<head>
<meta chrset="UTF 8">
<title>Event Registration</title>
</head>
<body>
<script src="https://code.angularjs.org/1.6.9/angular-route.js"></script>
<script src="https://code.angularjs.org/1.6.9/angular.min.js"></script>
<script src="https://code.angularjs.org/1.6.9/angular.js"></script>
<script src="/lib/bootstrap.js"></script>
<script src="/lib/bootstrap.css"></script>
<h1> gtupapers Global Event</h1>
<form ng-app="DemoApp" ng-controller="DemoController" name="myForm" novalidate>
<p>Topic Name:<br>
<input type="text" name="user" ng-model="user" required>
<span style="color:red" ng-show="myForm.user.$dirty && myForm.user.$invalid">
<span ng-show="myForm.user.$error.required">Username is required</span>
</span>
</p>
<p>
<input type="submit" ng-disabled="myForm.user.$dirty && myForm.user.$invalid">
</p>
</form>
<script>
var app = angular.module("DemoApp",[]);
app.controller("DemoController",function($scope) {
$scope.Display = function () {
$scope.user='Angular';
}
});
</script>
</body>
</html>
Code Explanation:
If the code is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
When the form is initially displayed, the textbox displays the value of "AngularJS" and the "submit button" is enabled. As soon as you remove the text from the control, the validation error message is enabled, and the Submit button is disabled.
The above screenshot displays two things
There is a facility in AngularJS to have validated all controls on a form automatically without needing to write custom code for the validation. This can be done by including a custom module called "jcs-AutoValidate."
With this module in place, you don't need to place any special code to carry out the validation or show the error messages. This is all handled by the code inside of the JCS-AutoValidate.
Let's look at a simple example of how to achieve this.
In this example,
We are just going to have a simple form with a textbox control which is a required field. An error message should be displayed if this control is not filled in.
<!DOCTYPE html>
<html>
<head>
<meta chrset="UTF 8">
<title>Event Registration</title>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="https://code.angularjs.org/1.6.9/angular.js"></script>
<script src="/lib/jcs-auto-validate.min.js"></script>
<body>
<h1> gtupapers Event</h1>
<div ng-app="DemoApp">
<div class="form-group">
<form name="myForm" novalidate>
<p>Topic Name:<br></p>
<div class="form-group">
<input class="form-control" type="text" id="user" ng-model="user" required="required"/>
</div>
<div>
<div class="form-group">
<input type="submit">
</div>
</div>
</form>
</div>
</div>
<script>
var app=angular.module('DemoApp',['jcs-autoValidate']);
</script>
</body>
</html>
Code Explanation:
If the code is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
By default when you run your code the above form will be shown as per the HTML code.
If you try to Submit the form, the error message will pop-up saying, "This field is required." All of this is done by the JCS-AutoValidate option.
The "ladda" buttons is a special framework built for buttons on top of JavaScript to give a visual effect to buttons when they are pressed.
So if a button is given the "ladda" attribute and is pressed, a spin effect will be shown. Also, there are different data styles available for the button to provide additional visual effects.
Let's look at an example, of how to see "ladda" buttons in action. We are just going to see a simple form which has a submit button. When the button is pressed, a spin effect will be shown on the button.
<!DOCTYPE html>
<html>
<head>
<meta chrset="UTF 8">
<title>Event Registration</title>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="https://code.angularjs.org/1.6.9/angular.js"></script>
<script src="/lib/jcs-auto-validate.min.js"></script>
<script src="/lib/angular-ladda.js"></script>
<script src="/lib/angular-ladda.min.js"></script>
<script src="/lib/bootstrap.js"></script>
<script src="/lib/bootstrap.css"></script>
<body>
<h1> gtupapers Event</h1>
<div ng-app="DemoApp" ng-controller="DemoController">
<div class="form-group">
<form name="myForm" novalidate ng-submit="submit()">
<div>
<button class="btn btn-primary" type="submit" ladda="submitting" name="sbutton" data-style="expand-right">Submit</button>
</div>
</form>
</div>
</div>
<script>
var app=angular.module('DemoApp',['jcs-autoValidate','angular-ladda']);
app.controller('DemoController',function($scope) {
$scope.submitting = false;
$scope.submit = function () {
$scope.submitting = true;
}
});
</script>
</body>
</html>
Code Explanation:
If the code is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
When the form is initially displayed, the submit button is shown in its simple form.
When the submit button is pressed, the submitting variable in the controller is set to true. This value gets passed to the "ladda" attribute of the submit button which causes the spin effect of the button.
Summary
What is $scope in AngularJS? $scope in AngularJS is a built-in object which basically binds the...
What is AngularJS? AngularJS is an open source Model-View-Controller framework which is similar to...
When creating web-based applications, sooner or later your application will need to handle DOM...
What is React JS? React is a Javascript library developed by Facebook which allows you to build UI...
What is Custom Directive? A custom directive in Angular Js is a user-defined directive with your...
What is an AngularJS directive? A directive in AngularJS is a command that gives HTML new...