本文實(shí)例講述了Angular簡單驗(yàn)證功能。分享給大家供大家參考,具體如下:
先來看看運(yùn)行效果:
完整實(shí)例代碼如下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>www.gxlcms.com angular驗(yàn)證功能</title> <script src="angular.min.js"></script> <style> input{ display: block; } ul li{ color: red; } </style> <script> angular.module("myapp",[]) .controller("demoC",function($scope){ $scope.datas = [{ id: 10011120, name: "iphoneX", num: 99 }, { id: 10011121, name: "華為mate10", num: 20 }, { id: 10011122, name: "vivoR12", num: 55 } ]; //定義一個(gè)數(shù)組 $scope.save=function(){ //創(chuàng)建一個(gè)存放錯(cuò)誤信息數(shù)組 $scope.error_val=[]; var reg_id=/^\d{8,8}$/; //只能8位數(shù)字 if(!reg_id.test($scope.id)){ $scope.error_val.push("資產(chǎn)編號(hào)格式,必須為數(shù)字,且長度為8位"); } //資產(chǎn)名稱 if($scope.name==undefined||$scope.name==""){ $scope.error_val.push("資產(chǎn)名稱不能為空!"); }else{ for(var i in $scope.datas){ if($scope.name==$scope.datas[i].name){ $scope.error_val.push("資產(chǎn)名稱已經(jīng)存在"); break; //結(jié)束循環(huán),已經(jīng)查找到資產(chǎn)名稱不合法 } } } //資產(chǎn)數(shù)量 var reg_num=/^\d{1,}$/; //只能8位數(shù)字 if(!reg_num.test($scope.num)){ $scope.error_val.push("資產(chǎn)編號(hào)數(shù)量,必須為數(shù)字"); }else{ if($scope.num<=0){ $scope.error_val.push("資產(chǎn)編號(hào)數(shù)量必須大于0"); } } //何時(shí)添加進(jìn)行,何時(shí)不添加 if($scope.error_val.length==0){ $scope.datas.push({ id:$scope.id, name:$scope.name, num:$scope.num }); } } }) </script> </head> <body ng-app="myapp" ng-controller="demoC"> <table border="1px solid"> <tr> <td>資產(chǎn)編號(hào)</td> <td>資產(chǎn)名稱</td> <td>資產(chǎn)數(shù)量</td> </tr> <tr ng-repeat="d in datas"> <td>{{d.id}}</td> <td>{{d.name}}</td> <td>{{d.num}}</td> </tr> </table> <p> <form> 資產(chǎn)編號(hào)<input ng-model="id" /> 資產(chǎn)名稱<input ng-model="name" /> 資產(chǎn)數(shù)量<input ng-model="num" /> <p> <ul> <li ng-repeat="e in error_val"> {{e}} </li> </ul> </p> <button ng-click="save()"> 資產(chǎn)錄入 </button> </form> </p> </body> </html>
上面是我整理給大家的,希望今后會(huì)對(duì)大家有幫助。
相關(guān)文章:
在vue + element中如何實(shí)現(xiàn)表格分頁
在VueRouter中如何使用導(dǎo)航守衛(wèi)用法
如何使用zTree樹形菜單
在Vue.js中如何實(shí)現(xiàn)可折疊的樹形菜單
在vue-cli中有關(guān)webpack3提速優(yōu)化方面的問題
詳細(xì)講解EasyUI中abs的使用
在zTree中如何實(shí)現(xiàn)交互選項(xiàng)卡
在EasyUI中如何使用accordion
在PHP中如何使用JSONAPI
在Vue.js中通過遞歸組件如何構(gòu)建樹形菜單
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com