SoFunction
Updated on 2025-04-04

Detailed explanation of AngularJS implementation form verification function

In ng, properties are provided for forms and spaces to verify the state of control interactions

Boolean type:

ng-valid form is set when validation is passed
ng-invalid Set when the form fails to pass verification
ng-pristine Settings when the form has not changed
ng-dirty Settings when there is any change in the form

Object:

$error

Notes:

① Add name attribute to form and form components
② Add ngModel to the form components you need
③Usage of attributes
myForm.t_age.dirty/pristine/valid/invalid/$error

Example:

<!DOCTYPE html>
<html ng-app="myApp">
<head lang="en">
 <meta charset="UTF-8">
 <title></title>
 <script src="js/"></script>
</head>
<body>

<div ng-controller="myCtrl">
 <form name="myForm" ng-submit="submitInfo()">
 <input type="text" name="t_name"
   ng-model="userName" required/>
 <span ng-show="myForm.t_name.$">
  The name cannot be empty
 </span>
 <br/>
 <input type="text" name="t_age"
   ng-model="userAge" required/>
 <span ng-show="myForm.t_age.$invalid">Verification failed</span>
 <span ng-show="myForm.t_age.$pristine">No input</span>

 <br/>
 <input
  ng-disabled="myForm.$invalid"
  type="submit" value="submit"/>
 </form>
</div>

<script>
 var app = ('myApp', ['ng']);

 ('myCtrl', function ($scope) {
 $ = function () {
  ($,$);
 }
 });
</script>
</body>
</html>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.