JavaScript

超轻量级php框架startmvc

AngularJS实现的获取焦点及失去焦点时的表单验证功能示例

更新时间:2020-06-12 20:54:01 作者:startmvc
本文实例讲述了AngularJS实现的获取焦点及失去焦点时的表单验证功能。分享给大家供大家参

本文实例讲述了AngularJS实现的获取焦点及失去焦点时的表单验证功能。分享给大家供大家参考,具体如下:


<!DOCTYPE html>
<html ng-app="formExample">
<head>
 <meta charset="UTF-8">
 <title></title>
 <script src="../js/angular.js"></script>
 <script>
 angular.module('formExample', [])
 .controller('FormController', ['$scope', function($scope)
 {
 $scope.userType = 'guest';
 $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>

运行效果:

AngularJS 获取焦点 失去焦点时 表单验证