AngularJS的稍复杂form验证

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Tair(兼容Redis),内存型 2GB
简介:

代码下载:https://files.cnblogs.com/files/xiandedanteng/angularjsSoccerFormCheck.rar

代码:

复制代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html ng-app="notesApp">
 <head>
  <title> New Document </title>
  <style>
    .username.ng-valid{
        background-color:lightgreen;
    }
    .username.ng-invalid{
        background-color:pink;
    }
    .userage.ng-valid{
        background-color:lightgreen;
    }
    .userage.ng-invalid{
        background-color:pink;
    }
    .usermail.ng-valid{
        background-color:lightgreen;
    }
    .usermail.ng-invalid{
        background-color:pink;
    }
    .userdate.ng-valid{
        background-color:lightgreen;
    }
    .userdate.ng-invalid{
        background-color:pink;
    }
    .usersn.ng-valid{
        background-color:lightgreen;
    }
    .usersn.ng-invalid{
        background-color:pink;
    }
    .userurl.ng-valid{
        background-color:lightgreen;
    }
    .userurl.ng-invalid{
        background-color:pink;
    }
  </style>
    <meta charset="gbk">
    <script src="angular1.4.6.min.js"></script>
 </head>

 <body ng-controller="MainCtrl as ctrl"> 
    <form ng-submit="ctrl.submit()" name="myForm">
        <table>
            <tr>
                <td width="50px">姓名:</td>
                <td>
                    <input type="text" class="username" name="uname" ng-model="ctrl.user.name" required ng-minlength="4"/>
                </td>
                <td>
                    <span ng-show="myForm.uname.$error.required">This a required field</span>
                    <span ng-show="myForm.uname.$error.minlength">Minimum length required is 4</span>
                    <span ng-show="myForm.uname.$invalid">This field is invalid</span>
                </td>
            </tr>

            <tr>
                <td width="50px">年龄:</td>
                <td>
                    <input type="number" class="userage" name="uage" ng-model="ctrl.user.age" required  ng-minlength="2"/>
                </td>
                <td>
                    <span ng-show="myForm.uage.$error.required">This a required field</span>
                    <span ng-show="myForm.uage.$error.minlength">Minimum length required is 2</span>
                    <span ng-show="myForm.uage.$invalid">This field is invalid</span>
                </td>
            </tr>

            <tr>
                <td width="50px">邮件:</td>
                <td>
                    <input type="email" class="usermail" name="umail" ng-model="ctrl.user.mail" required  ng-minlength="3"/>
                </td>
                <td>
                    <span ng-show="myForm.umail.$error.required">This a required field</span>
                    <span ng-show="myForm.umail.$error.minlength">Minimum length required is 3</span>
                    <span ng-show="myForm.umail.$invalid">This field is invalid</span>
                </td>
            </tr>

            <tr>
                <td width="50px">入职日期:</td>
                <td>
                    <input type="date" class="userdate" name="udate" ng-model="ctrl.user.date" required  ng-minlength="8"/>
                </td>
                <td>
                    <span ng-show="myForm.udate.$error.required">This a required field</span>
                    <span ng-show="myForm.udate.$error.minlength">Minimum length required is 8</span>
                    <span ng-show="myForm.udate.$invalid">This field is invalid</span>
                </td>
            </tr>

            <tr>
                <td width="50px">SN:</td>
                <td>
                    <input type="text" class="usersn" name="usn" ng-model="ctrl.user.sn" ng-pattern="/^SN-\d{4}$/"/>
                </td>
                <td>
                    <span ng-show="myForm.udate.$invalid">This field is invalid</span>
                </td>
            </tr>

            <tr>
                <td width="50px">URL:</td>
                <td>
                    <input type="url" class="userurl" name="uurl" ng-model="ctrl.user.url" />
                </td>
                <td>
                    <span ng-show="myForm.uurl.$invalid">This field is invalid</span>
                </td>
            </tr>

            <tr>
                <td ></td>
                <td colspan="2"><input type="submit" value="Submit" ng-disabled="myForm.$invalid"/></td>
                <td>
            </tr>
        </table>
    </form>
 </body>
</html>
<script type="text/javascript">
<!--
    angular.module('notesApp',[])
     .controller('MainCtrl',[function(){
       var self=this;

       self.submit=function(){
            console.log("User name="+self.user.name+
                         " age="+self.user.age+
                         " mail="+self.user.mail+
                         " date="+self.user.date+
                         " sn="+self.user.sn+
                         " url="+self.user.url);
            alert("Form submitted.");
       };
       
     }]);
//-->
</script>
复制代码

效果:

要点:

目前我感觉AngularJS对复杂表单的验证不是那么方便,对浏览器的依耐性也强。常规验证的JS代码其实不复杂,AngularJS的优势在此并不明显。















本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/xiandedanteng/p/7419018.html,如需转载请自行联系原作者

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
JavaScript 前端开发
AngularJS ng-if使用方法详解
AngularJS ng-if使用方法详解
180 1
|
JavaScript
AngularJS ng-if使用
AngularJS ng-if使用
|
JavaScript 前端开发 API
让Angular自定义组件支持form表单验证
让Angular自定义组件支持form表单验证
160 0
|
数据格式 JSON JavaScript
|
数据安全/隐私保护