代码下载: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的优势在此并不明显。

最新文章

  1. zabbix-proxy3.0.4编译安装
  2. UDKtoUE4Tool-UDKUE3资源移植UE4工具
  3. Sql Server 数据库之间如何进行跨网远程连接访问
  4. InterlliJ调试:Method breakpoints may dramatically slow down debugging
  5. Bluetooth LE(低功耗蓝牙) - 第五部分
  6. Jira在linux上安装与部署
  7. OpenCV五学习: 如何使用命令来启动或关闭OpenCV的CPU指令系统CV_SSE2,CV_SSSE4和其他优化
  8. 获取子物体数量---Transform.childCount
  9. laravel 自动加载 自定义的文件/辅助函数
  10. ansbile Tags &amp;&amp; Block
  11. GitHub linux 提交文件及403错误处理
  12. H3C交换机端口安全技术之端口隔离的应用
  13. C++语言实现-邻接矩阵
  14. lua第三方库
  15. oracle创建表空间 授权
  16. web3.js编译Solidity,发布,调用全部流程(手把手教程)
  17. Python算法基础
  18. Vue.js简介及指令
  19. jQuery mobile 初始化页面的过程
  20. NodeJs实现简单的爬虫

热门文章

  1. 在smarty模板中使用PHP函数的方法
  2. 【bzoj1367】[Baltic2004]sequence 可并堆
  3. 【Luogu】P3320寻宝游戏(Splay)
  4. tmux使用备忘
  5. [ACG001E] BBQ hard [dp]
  6. jQuery 之 验证表单
  7. pat 团体天梯赛 L2-012. 关于堆的判断
  8. HDU [P3849]
  9. 【HDOJ5555】Immortality of Frog(状压DP)
  10. linux下解压zip文件