Home >Web Front-end >JS Tutorial >How to implement form validation function when AngularJS gets focus and loses focus

How to implement form validation function when AngularJS gets focus and loses focus

小云云
小云云Original
2018-01-22 10:01:561874browse

This article mainly introduces the form verification function implemented by AngularJS to obtain focus and lose focus. It involves AngularJS's use of ng-blur and ng-focus to monitor form event related operating techniques. Friends who need it can refer to it. I hope it can help. Everyone.

The example in this article describes the form validation function implemented by AngularJS to obtain focus and lose focus. Share it with everyone for your reference, the details are as follows:


<!DOCTYPE html>
<html ng-app="formExample">
<head>
  <meta charset="UTF-8">
  <title></title>
  <script src="../js/angular.js"></script>
  <script>
    angular.module(&#39;formExample&#39;, [])
        .controller(&#39;FormController&#39;, [&#39;$scope&#39;, function($scope)
        {
          $scope.userType = &#39;guest&#39;;
          $scope.change = false;
        }]);
  </script>
</head>
<body>
<form name="myForm" ng-controller="FormController">
  userType: <input name="input" ng-model="userType" ng-blur="change=true" ng-focus="change=false" required>
  <span class="error" ng-show="myForm.input.$error.required && change">必填项</span><br>
</form>
</body>
</html>

Running effect:

Related recommendations:

Perfect solution to the problem that input cannot get focus when BootStrap modal box and select2 are used together

blur() loses focus and focus() gets focus event in jquery

javascript - How to solve the problem of input getting focus and soft keyboard blocking input in Apple 5


The above is the detailed content of How to implement form validation function when AngularJS gets focus and loses focus. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn