AngularJS表单和输入验证实例

雨伟大大_9661

雨伟大大_9661

2016-12-08

1252人浏览

原创

angularjs 表单和控件可以提供验证功能,并对用户输入的非法数据进行警告。

注意:客户端的验证不能确保用户输入数据的安全,所以服务端的数据验证也是必须的。

1、HTML 控件

以下 HTML input 元素被称为 HTML 控件:input 元素、select 元素、button 元素、textarea 元素。

2、HTML 表单

 AngularJS 表单是输入控件的集合。HTML 表单通常与 HTML 控件同时存在。

html>


<meta><title>angularJs</title><script></script><div>
 <form>
  First Name:<br>
  <input><br>
  Last Name:<br>
  <input>
  <br><br>
  <button>RESET</button>
 </form>
 <p>form = {{user }}</p>
 <p>master = {{master}}</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('formCtrl', function($scope) {
  $scope.master = {firstName:"John", lastName:"Doe"};
  $scope.reset = function() {
    $scope.user = angular.copy($scope.master);
  };
  $scope.reset();
});
</script>

   

阿里云AI学习路线
阿里云AI学习路线

阿里云AI学习路线是一款AI学习资源工具,阿里云推出的人工智能学习路线(学+测)。

下载

3、输入验证

AngularJS 表单和控件可以提供验证功能,并对用户输入的非法数据进行警告。客户端的验证不能确保用户输入数据的安全,所以服务端的数据验证也是必须的。

html>


<meta><title>angularJs</title><script></script><h2>验证实例</h2>

用户名: 用户名是必须的。

邮  箱: 邮箱是必须的。 非法的邮箱地址。

<script> var&nbsp;app&nbsp;=&nbsp;angular.module('myApp',&nbsp;[]); app.controller('validateCtrl',&nbsp;function($scope)&nbsp;{ &nbsp;&nbsp;$scope.user&nbsp;=&nbsp;'John&nbsp;Doe'; &nbsp;&nbsp;$scope.email&nbsp;=&nbsp;'john.doe@gmail.com'; }); </script>

   

AngularJS ng-model 指令用于绑定输入元素到模型中。模型对象有两个属性: user 和 email。我们使用了 ng-show指令,color:red 在邮件是 $dirty 或 $invalid 才显示。

2016112141933913.png

没用初始值的输入验证:注意ng-app="",ng-app有值就必须连接到代码模块,利用angular.module 函数来创建模块。

html>


<meta><title>angularJs</title><script></script><h2>验证实例</h2>

用户名: 用户名是必须的。

邮  箱: 邮箱是必须的。 非法的邮箱地址。

   

4、ng-disabled实例

html>

  
    <script></script>
  
  
    <div>
      <form>
        Name:
        <input><br>
        E-mail:
        <input><br>
        <div>
          Invalid:
          <span>Tell us your email.</span>
          <span>This is not a valid email.</span>
        </div>
        Gender:<br>
        <input>
        male
        <input>
        female<br>
        <input>
        I agree:
        <input>
        <div>
          Please agree and sign.
        </div>
        <br>
        <!--ng-disabled为true时禁止使用,ng-disabled实时监控应用程序-->
        <button>
          RESET
        </button>
        <button>
          SAVE
        </button>
      </form>
    </div>
  <script>
    var app=angular.module("MyApp",[]);
    app.controller("Controller",function($scope){
      $scope.master = {};
      $scope.update=function(user){
        $scope.master=$scope.copy(user);
      };
      $scope.reset=function(){
        $scope.user=angular.copy($scope.master);
      };
      $scope.isUnchanged=function(user){
        //判断user和$scope.master是否相等,相等返回true,否则返回false
        return angular.equals(user,$scope.master);
      };
      $scope.reset();
    });
  </script>
  

   

5、ng-submit实例

  
    <meta>
    <script></script>
  
  <!--ng-submit绑定表单提交事件-->
    
               
            
     <script> &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;app=angular.module("TestFormModule",[]); &nbsp;&nbsp;&nbsp;&nbsp;app.controller("TestFormModule",function($scope){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$scope.user={ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userName:"&#23665;&#27700;&#23376;&#20892;", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;password:'' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$scope.save=function(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.log("&#20445;&#23384;&#25968;&#25454;&#20013;..."); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;</script>

   

6、maxlength和minlength实例

html>

  
    <meta>
    <script></script>
    <title>表单验证</title>
  
  
  <div>
  <form>
   <label>Set a maxlength: </label>
   <input><br>
   <label>Set a minlength: </label>
   <input><br><hr>
   <label>This input is restricted by the current maxlength and minlength: </label><br>
   <input><br>
   text input valid? = <code>{{form.text.$valid}}</code><br>
   text model = <code>{{textmodel}}</code><br><hr>
   <label>This input is restricted by the current maxlength and minlength: </label><br>
   <input><br>
   number input valid? = <code>{{form.number.$valid}}</code><br>
   number model = <code>{{numbermodel}}</code>
  </form>
  </div>  
  <script>
    var app=angular.module("myApp",[]);
    app.controller("myCtrl",function($scope){
      $scope.maxlength=8;
      $scope.minlength=4;
    });
  </script>
  

   

7、ng-class实例

   
html>

  
    <meta>
    <script></script>
    <title>表单验证</title>
    <style>
    .deleted {
     text-decoration: line-through;
    }
    .bold {
     font-weight: bold;
    }
    .red {
     color: red;
    }
    .error {
     color: red;
     background-color: yellow;
    }
    .base-class {
     transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s;
    }
    .base-class.my-class {
     color: red;
     font-size:3em;
    }
    </style>
  
  
  <div>
  <form>
    <p>Map Syntax Example</p>
    <label><input>deleted (apply "deleted" class)</label><br>
    <label><input>bold (apply "bold" class)</label><br>
    <label><input>error (apply "error" class)</label>
    <hr>
    <input>
    <input>
    <br>
    <span>Sample Text</span>
  </form>
  </div>  
  <script>
    var app=angular.module("myApp",[]);
    app.controller("myCtrl",function($scope){
    });
  </script>
  
<strong>
</strong>

 2016112141933913.png

8、ng-if实例

html>

  
    <meta>
    <script></script>
    <title>表单验证</title>
    <style>
    .animate-if {
     width:400px;
     border:2px solid yellowgreen;
     border-radius: 10px;
     padding:10px;
     display: block;
    } 
    </style>
  
  
  <div>
  <form>
    <label>Click me: <input></label><br>
    Show when checked:
    <span>
    This is removed when the checkbox is unchecked.
    </span>
  </form>
  </div>  
  <script>
    var app=angular.module("myApp",[]);
    app.controller("myCtrl",function($scope){
    });
  </script>
  

   


相关文章

PHP速学视频免费教程(入门到精通)
PHP速学视频免费教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
GoFrame框架Web开发与路由中间件
GoFrame框架Web开发与路由中间件

本专题聚焦GoFrame框架的Web开发,拆解路由注册与匹配规则、请求参数获取、参数校验与中间件执行顺序,说明统一响应、JWT鉴权、文件上传与接口文档生成的落地方式,并补充优雅重启与测试实践。

2026.11.17

0

15

GoFrame框架安装与gf命令行工具
GoFrame框架安装与gf命令行工具

本专题围绕GoFrame框架的安装与工具链配置展开,讲清Go版本要求、gf命令行的安装方式、代理镜像与依赖准备,说明项目初始化、热编译、交叉编译打包的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.17

0

15

GoFrame框架零基础入门
GoFrame框架零基础入门

本专题整理GoFrame框架的入门知识,从模块化设计理念、环境准备到跑通第一个Web服务,讲清目录分层、配置加载、日志与中间件的用法,并对比Gin、Go-Zero的差异,帮助Go开发者建立整体认知。

2026.11.17

0

15

SQLAlchemy 一对多关系怎么建立
SQLAlchemy 一对多关系怎么建立

SQLAlchemy 一对多关系怎么建立?本专题整理父子表外键、relationship 双向映射、关联查询、级联删除和加载优化等相关文章,帮助你理解并实现常见的 ORM 关系建模。

2026.11.17

0

10

SQLAlchemy 如何增删改查
SQLAlchemy 如何增删改查

SQLAlchemy 如何增删改查?本专题整理 ORM 新增记录、条件查询、更新和删除、Session 事务提交与批量处理等实用文章,帮助你掌握 Python 数据库操作流程。

2026.11.17

0

11

SQLAlchemy ORM 模型怎么写
SQLAlchemy ORM 模型怎么写

SQLAlchemy ORM 模型怎么写?本专题整理模型类定义、Mapped 字段映射、主键与约束、表结构创建和常见报错的相关文章,帮助你从基础配置到实际建模逐步掌握 SQLAlchemy。

2026.11.17

0

16

Go-Zero API开发使用教程
Go-Zero API开发使用教程

本专题聚焦Go-Zero的API开发,拆解.api描述文件的语法结构、路由与分组定义、参数绑定与校验规则,说明统一响应体、JWT鉴权、跨域与接口文档生成的落地方式,并给出网关聚合与接口测试的实践建议。

2026.11.12

160

15

Go-Zero安装与goctl环境配置教程
Go-Zero安装与goctl环境配置教程

本专题围绕Go-Zero的安装与工具链配置展开,讲清Go版本要求、goctl安装方式、代理镜像、protoc依赖与环境变量设置,说明多版本共存、升级回退与CI流水线安装的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.12

120

15

Go-Zero零基础入门教程
Go-Zero零基础入门教程

本专题整理Go-Zero的入门知识,从框架定位、环境准备到跑通第一个API服务,讲清目录分层、配置加载、日志与中间件的基本用法,并对比Gin、Kratos的差异,帮助Go开发者建立整体认知,判断它是否适合当前项目。

2026.11.12

100

15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
AngularJS教程
AngularJS教程

共24课时 | 7.9万人学习

走进 ES6 新标准语法
走进 ES6 新标准语法

共15课时 | 2万人学习

AngularJS 中文手册
AngularJS 中文手册

共0课时 | 0.5万人学习