AngularJS Validation

AngularJS is an open source script language. AngularJS validation has fast processing compared to other validations. Hence, AngularJS works on the client-side form validation. One of the best features of AngularJS is the form validation.

Validate data

These are some functions, which can be used to track the error.

  • $dirty – It is used by the user to make some changes.
  • $invalid – It is used to identify the value entered is invalid.
  • $error – It is used to exact the error.
  • $Valid- These validations in the form currently evaluates to correct.
Let’s start to our program

Here, I am using Visual Studio 2013 to write an AngularJS program. You can write some other text such as (sublime text, Notepad++,Text….). Thus, many text options are available to write the program.

Step 1: Open Visual Studio, click New=>Project.

1

Step 2:
Now, select ASP.NET Web Application. Create a project name. Here, our project name is “FormValidation “ and click OK button.

2

Step 3: Select MVC template and click OK button.

3

Step 4: Here, right click on the project name and add=> HTML page.

4

Step 5: Now, right click on the project name. Click on NuGet packages.

5

Step 6: You need to search AnjularJS in the TextBox. nstall AngularJS.

6

Step 7: In this section , I am using CSS and AngularJS Validation coding.
  1. <!DOCTYPE html>
  2. <html ng-app="myForm"
  3. xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>AngularJS Validation demo</title>
  6. <style>
  7. body {
  8. font-family: "Calibri";
  9. background-color: #E2E2DC;
  10. }
  11. </style>
  12. <style>
  13. .name.ng-valid {
  14. background-color: lightgreen;
  15. }
  16. .name.ng-dirty.ng-invalid-required {
  17. background-color: red;
  18. }
  19. .name.ng-dirty.ng-invalid-minlength {
  20. background-color: orange;
  21. }
  22. .degree.ng-valid {
  23. background-color: lightgreen;
  24. }
  25. .degree.ng-dirty.ng-invalid-required {
  26. background-color: red;
  27. }
  28. degree.ng-dirty.ng-invalid-minlength {
  29. background-color: orange;
  30. }
  31. .email.ng-valid {
  32. background-color: lightgreen;
  33. }
  34. .email.ng-dirty.ng-invalid-required {
  35. background-color: red;
  36. }
  37. .email.ng-dirty.ng-invalid-email {
  38. background-color: orange;
  39. }
  40. .Mnumber.ng-valid {
  41. background-color: lightgreen;
  42. }
  43. .Mnumber.ng-dirty.ng-invalid-required {
  44. background-color: red;
  45. }
  46. .Mnumber.ng-dirty.ng-invalid-minlength {
  47. background-color: orange;
  48. }
  49. </style>
  50. </head>
  51. <body ng-controller="Mycontroller as Myctrl">
  52. <form ng-submit="Myctrl.submit()" name="MyInfo">
  53. <label>Name:</label>
  54. <input type="text"ng-model="Myctrl.user.name" name="Name" class="name" placeholder="Enter your Name" required ng-minlength="4">
  55. <span ng-show="MyInfo.$dirty && MyInfo.Name.$error.required">Name is Required</span>
  56. <span ng-show="MyInfo.$dirty && MyInfo.Name.$error.minlength">Minimum length Required is 4</span>
  57. <span ng-show="MyInfo.$dirty && MyInfo.Name.invalid">This field is Invalid</span>
  58. <br />
  59. <br />
  60. <label>Degree:</label>
  61. <input type="text" ng-model="Myctrl.user.degree" name="Degree" class="degree" placeholder="Enter your Degree Name" required ng-minlength="2">
  62. <span ng-show="MyInfo.$dirty && MyInfo.Degree.$error.required">Degree is Required</span>
  63. <span ng-show="MyInfo.$dirty && MyInfo.Degree.$error.minlength">Minimum length Required is 2</span>
  64. <span ng-show="MyInfo.$dirty && MyInfo.Degree.invalid">This field is Invalid</span>
  65. <br />
  66. <br />
  67. <label>EmailId:</label>
  68. <input type="email" ng-model="Myctrl.user.email" name="email" class="email" placeholder="Enter your Email" required />
  69. <span ng-show="MyInfo.$dirty && MyInfo.email.$error.required">Email is Required</span>
  70. <span ng-show="MyInfo.$dirty && MyInfo.email.invalid">This field is Invalid</span>
  71. <br />
  72. <br />
  73. <label>Mnumber:</label>
  74. <input type="number" ng-model="Myctrl.user.Mnumber" name="MNumber" class="Mnumber" placeholder="Enter your Mobile Number " required ng-minlength="10">
  75. <span ng-show="MyInfo.$dirty && MyInfo.MNumber.$error.required">Mobile Number is Required</span>
  76. <span ng-show="MyInfo.$dirty && MyInfo.MNumber.$error.minlength">Minimum length Required is 10</span>
  77. <span ng-show="MyInfo.$dirty && MyInfo.MNumber.invalid">This field is Invalid</span>
  78. <br />
  79. <br />
  80. <input type="submit" value="Submit" ng-disabled="MyInfo.$invalid">
  81. </form>
  82. <script src="Scripts/angular.js"></script>
  83. <script>
  84. angular.module('myForm', [])
  85. .controller('Mycontroller', [function () {
  86. var self = this;
  87. self.submit = function () {
  88. console.log('Form is submitted with following user', self.user);
  89. };
  90. }]);
  91. </script>
  92. </body>
  93. </html>
Output: Run the Application and show the output.

output 1

Here, AngularJS validation is working properly.

output 2

Here, you can see the range validation in AngularJS.

output 3

Finally, our output is successfully shown in the figure, given below:

output 4