AngularJs表單校驗功能實例代碼
廢話不多說了,具體代碼如下所示:
<!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,不然無法執(zhí)行在臟檢查,就無法校驗-->
<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檢查是否是第一次輸入!網(wǎng)上有很多種方法校驗是否是第一次輸入-->
<span class="warning" ng-show="angularForm.userName.$dirty && angularForm.userName.$error.required">*</span>
<span class="warning" ng-show="angularForm.userName.$error.number">只能輸入數(shù)字</span>
<span class="warning" ng-show="angularForm.userName.$error.minlength">最少為6位數(shù)</span>
</td>
</tr>
<tr>
<td>密碼</td>
<td>
<!--這里的id,一定要等于compare-pwd的值,因為指令里面是根據(jù)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位數(shù)</span>
</td>
</tr>
<tr>
<td>確認(rèn)密碼</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,是指令里面設(shè)置的-->
<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正則表達(dá)式校驗輸入內(nèi)容-->
<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">只能輸入數(shù)字</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>
js代碼(除了指令意外,沒什么可用的,寫出來只是為了,說一下mvc模式而已?。?/p>
var app = angular.module("angularFormCheckModule",[]);
/*這里使用MVC的模式(用來舉例說明MVC而已)*/
app.controller("angularFormCheckCtrl",function($scope,angularFormCheckFactory){//function里的參數(shù)寫你在函數(shù)里需要用到的
$scope.testVar = angularFormCheckFactory.getTest();//這里就能取到$scope.testVar的值為---"練習(xí)angular表單校驗";
$scope.user = {};
$scope.test= "sss";
});
/*自己可以去看factory、service、providers的區(qū)別(http://www.oschina.net/translate/angularjs-factory-vs-service-vs-provider)*/
/*用 Factory 就是創(chuàng)建一個對象,為它添加屬性,然后把這個對象返回出來。*/
app.factory('angularFormCheckFactory',function(){
//這里寫自己的業(yè)務(wù)邏輯
var test = "練習(xí)angular表單校驗";
var service = {};//自定義一個對象
service.getTest = function(){//給對象添加方法
return test;
}
return service;//返回自定義的service對象?。。?
});
/*自定義指令--比較兩個密碼是否相等.angular的指令是駝峰的形式(這里是comparePwd頁面就是compare-pwd)*/
app.directive('comparePwd',function(){
/*angular 自定義指令,可上網(wǎng)自行查找*/
return{
require : 'ngModel',
/*scope表示作用域,elem表示使用這個指令的元素對象(這里指第二個密碼框),attrs。。。ctrl。。。*/
link : function(scope,elem,attrs,ctrl){
/*寫自己的業(yè)務(wù)邏輯*/
//注意這樣取值的話,第一密碼框的Id值必須要設(shè)置且必須與第二個密碼框的compare-pwd屬性的值相同
var firstPwdIdObj = "#" + attrs.comparePwd;
$(elem).add(firstPwdIdObj).on('keyup',function(){
/*手動執(zhí)行臟檢查*/
scope.$apply(function(){
//$(firstPwdIdObj).val()表示第一個密碼框的值。elem.val()表示第二個密碼框的值
var flag = elem.val() === $(firstPwdIdObj).val();
//alert(flag+",--"+elem.val()+",--"+$(firstPwdIdObj).val());
ctrl.$setValidity("pwdmatch",flag);//flag,表示是否相等。pwdmatch用于$error時的標(biāo)識符,注意看頁面,$setValidity是require中ngModel的方法!
});
});
}
}
});
下面看一段代碼關(guān)于AngularJs獲取焦點與失去焦點時的表單驗證
<!DOCTYPE html>
<html ng-app="formExample">
<head>
<meta charset="UTF-8">
<title></title>
<script src="../js/angular.js"></script>
<script>
angular.module('formExample', [])
.controller('FormController', ['$scope', function($scope)
{
$scope.userType = 'guest';
$scope.change = false;
}]);
</script>
</head>
<body>
<form name="myForm" ng-controller="FormController">
userType: <input name="input" ng-model="userType" ng-blur="change=true" ng-focus="change=false" required>
<span class="error" ng-show="myForm.input.$error.required && change">必填項</span><br>
</form>
</body>
</html>
以上所述是小編給大家介紹的AngularJs表單校驗功能實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Angular統(tǒng)一注入器unified injector簡化依賴關(guān)系管理
這篇文章主要為大家介紹了Angular統(tǒng)一注入器unified injector簡化依賴關(guān)系管理的使用方法實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
Angular設(shè)計模式hierarchical?injector實現(xiàn)代碼復(fù)用模塊化
這篇文章主要為大家介紹了Angular設(shè)計模式hierarchical?injector實現(xiàn)代碼復(fù)用模塊化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
Angular懶加載動態(tài)創(chuàng)建顯示該模塊下聲明的組件
這篇文章主要為大家介紹了Angular懶加載動態(tài)創(chuàng)建顯示該模塊下聲明的組件,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05
詳解angular路由高亮之RouterLinkActive
這篇文章主要介紹了詳解angular路由高亮之RouterLinkActive,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
angularjs $http調(diào)用接口的方式詳解
今天小編就為大家分享一篇angularjs $http調(diào)用接口的方式詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
AngularJs Using $location詳解及示例代碼
本文主要介紹AngularJs Using $location的知識資料,這里整理了相關(guān)的資料,及簡單示例代碼,有興趣的小伙伴可以參考下2016-09-09
在Angular中使用NgTemplateOutlet創(chuàng)建可重用組件的流程步驟
在 Angular 中,使用 NgTemplateOutlet 而不是創(chuàng)建特定組件,可以使組件在不修改組件本身的情況下輕松修改為各種用例,在本文中,您將接受一個現(xiàn)有組件并重寫它以使用 NgTemplateOutlet,需要的朋友可以參考下2024-03-03
Angular實現(xiàn)點擊按鈕控制隱藏和顯示功能示例
這篇文章主要介紹了Angular實現(xiàn)點擊按鈕控制隱藏和顯示功能,結(jié)合實例形式分析了AngularJS簡單控制頁面元素顯示與隱藏的相關(guān)操作技巧,需要的朋友可以參考下2017-12-12

