废话不多说了,具体代码如下所示:
<!DOCTYPE html> <html ng-app="angularFormCheckModule"> <head> <meta charset="UTF-8"> <title>angular表单校验</title> <link rel="stylesheet" href="../css/bootstrap.min.css" rel="external nofollow" /> <style> span{ color: red; } </style> </head> <body ng-controller="angularFormCheckCtrl"> <!--使用angular校验,每一个校验的项都必须用ng-model,不然无法执行在脏检查,就无法校验--> <form name="angularForm" novalidate method="post"> <table class="table table-bordered"> <tr> <td>用户名</td> <td> <input type="number" required="required" ng-model="user.userName" name="userName" ng-minlength="6"/> <!--angularForm.userName.$dirty检查是否是第一次输入!网上有很多种方法校验是否是第一次输入--> <span class="warning" ng-show="angularForm.userName.$dirty && angularForm.userName.$error.required">*</span> <span class="warning" ng-show="angularForm.userName.$error.number">只能输入数字</span> <span class="warning" ng-show="angularForm.userName.$error.minlength">最少为6位数</span> </td> </tr> <tr> <td>密码</td> <td> <!--这里的id,一定要等于compare-pwd的值,因为指令里面是根据Id取值的--> <input type="password" required="required" ng-minlength="6" name="pwd" ng-model="user.password" id="pwd"/> <!--angularForm.pwd.$pristine首次输入,不太清楚的就自己运行,去掉条件一个一个的试!--> <span class="warning" ng-show="!angularForm.pwd.$pristine && angularForm.pwd.$error.required">*</span> <span class="warning" ng-show="angularForm.pwd.$error.minlength">最少为6位数</span> </td> </tr> <tr> <td>确认密码</td> <td> <!--这里compare-pwd的值,要等于被比较的对象的name属性值,即第一个密码框的name值--> <input type="password" required="required" name="pwd2" compare-pwd="pwd" ng-model="pwd2"/> <span class="warning" ng-show="angularForm.pwd2.$error.required">*</span> <!--注意这里的pwdmatch,是指令里面设置的--> <span class="warning" ng-show="angularForm.pwd2.$error.pwdmatch">X</span> <span class="warning" ng-show="angularForm.pwd2.$valid" style="color: green;">OK</span> <!-- 其实这种事最简单的校验方式,不用写指令!!! <span ng-show="user.password !=pwd2">两次密码输入不一致</span> --> </td> </tr> <tr> <td>手机</td> <td> <!--pattern正则表达式校验输入内容--> <input type="number" required="required" name="phone" ng-model="user.phone" ng-pattern="/^1[3|4|5|7|8]d{9}$/"> <span class="warning" ng-show="angularForm.phone.$error.required">*</span> <span class="warning" ng-show="angularForm.phone.$error.number">只能输入数字</span> <span class="warning" ng-show="angularForm.phone.$error.pattern">手机格式不正确</span> </td> </tr> <tr> <td>邮箱</td> <td> <input type="email" required="required" ng-model="user.email" name="email"/> <span class="warning" ng-show="angularForm.email.$error.required">*</span> <span class="warning" ng-show="angularForm.email.$error.email">邮箱格式不正确</span> </td> </tr> <tr> <td>URL</td> <td> <input type="url" required="required" ng-model="user.url" name="url"/> <span class="warning" ng-show="angularForm.url.$error.required">*</span> <span class="warning" ng-show="angularForm.url.$error.url">URL格式不正确</span> </td> </tr> <tr> <td>(注:*为必填)</td> <td> <input type="submit" value="提交" ng-disabled="!angularForm.$valid" class="btn btn-success"/> </td> </tr> </table> </form> </body> <script type="text/javascript" src="../js/jquery.min.js" ></script> <script type="text/javascript" src="../js/angular-1.2.22.js" ></script> <script type="text/javascript" src="../js/angularFormCheck.js" ></script> </html>更多相关内容:
-
无相关信息