AngularJS 輸入驗(yàn)證詳解及實(shí)例代碼
AngularJS 輸入驗(yàn)證
AngularJS 表單和控件可以驗(yàn)證輸入的數(shù)據(jù)。
輸入驗(yàn)證
在前面的幾個章節(jié)中,你已經(jīng)學(xué)到關(guān)于 AngularJS 表單和控件的知識。
AngularJS 表單和控件可以提供驗(yàn)證功能,并對用戶輸入的非法數(shù)據(jù)進(jìn)行警告。
注意: 客戶端的驗(yàn)證不能確保用戶輸入數(shù)據(jù)的安全,所以服務(wù)端的數(shù)據(jù)驗(yàn)證也是必須的。
應(yīng)用代碼
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <h2>驗(yàn)證實(shí)例</h2> <form ng-app="myApp" ng-controller="validateCtrl" name="myForm" novalidate> <p>用戶名:<br> <input type="text" name="user" ng-model="user" required> <span style="color:red" ng-show="myForm.user.$dirty && myForm.user.$invalid"> <span ng-show="myForm.user.$error.required">用戶名是必須的。</span> </span> </p> <p>郵箱:<br> <input type="email" name="email" ng-model="email" required> <span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid"> <span ng-show="myForm.email.$error.required">郵箱是必須的。</span> <span ng-show="myForm.email.$error.email">非法的郵箱地址。</span> </span> </p> <p> <input type="submit" ng-disabled="myForm.user.$dirty && myForm.user.$invalid || myForm.email.$dirty && myForm.email.$invalid"> </p> </form> <script> var app = angular.module('myApp', []); app.controller('validateCtrl', function($scope) { $scope.user = 'John Doe'; $scope.email = 'john.doe@gmail.com'; }); </script> </body> </html>
運(yùn)行結(jié)果:
驗(yàn)證實(shí)例
用戶名:
郵箱:
注意: HTML 表單屬性 novalidate 用于禁用瀏覽器默認(rèn)的驗(yàn)證。
實(shí)例解析
AngularJS ng-model 指令用于綁定輸入元素到模型中。
模型對象有兩個屬性: user 和 email。
我們使用了 ng-show指令, color:red 在郵件是 $dirty 或 $invalid 才顯示。
屬性 | 描述 |
---|---|
$dirty | 表單有填寫記錄 |
$valid | 字段內(nèi)容合法的 |
$invalid | 字段內(nèi)容是非法的 |
$pristine | 表單沒有填寫記錄 |
以上就是對AngularJS 輸入驗(yàn)證的資料整理,后續(xù)繼續(xù)補(bǔ)充,希望能幫助學(xué)習(xí)的同學(xué)。
相關(guān)文章
使用 Angular RouteReuseStrategy 緩存(路由)組件的實(shí)例代碼
這篇文章主要介紹了使用 Angular RouteReuseStrategy 緩存(路由)組件的實(shí)例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11AngularJs 最新驗(yàn)證手機(jī)號碼的實(shí)例,成功測試通過
下面小編就為大家分享一篇AngularJs 最新驗(yàn)證手機(jī)號碼的實(shí)例,成功測試通過,具有很好的參考價值。希望對大家有所幫助。一起跟隨小編過來看看吧2017-11-11Angular設(shè)計模式hierarchical?injector實(shí)現(xiàn)代碼復(fù)用模塊化
這篇文章主要為大家介紹了Angular設(shè)計模式hierarchical?injector實(shí)現(xiàn)代碼復(fù)用模塊化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10AngularJs Understanding the Model Component
本文主要介紹AngularJs Understanding the Model Component的內(nèi)容,這里整理了相關(guān)資料,并詳細(xì)講解了這部分知識,有興趣的小伙伴可以參考下2016-09-09Angular實(shí)現(xiàn)的敏感文字自動過濾與提示功能示例
這篇文章主要介紹了Angular實(shí)現(xiàn)的敏感文字自動過濾與提示功能,結(jié)合實(shí)例形式分析了AngularJS針對字符串的輸入判定及實(shí)時顯示相關(guān)操作技巧,需要的朋友可以參考下2017-12-12在 Angular2 中實(shí)現(xiàn)自定義校驗(yàn)指令(確認(rèn)密碼)的方法
這篇文章給大家探索 Angular 2 內(nèi)建的自定義驗(yàn)證,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-01-01詳解angular2采用自定義指令(Directive)方式加載jquery插件
本篇文章主要介紹了詳解angular2采用自定義指令(Directive)方式加載jquery插件 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02Angular 4根據(jù)組件名稱動態(tài)創(chuàng)建出組件的方法教程
組件是我們在學(xué)習(xí)angular中必不可少的一部分,下面這篇文章主要給大家介紹了關(guān)于Angular 4如何根據(jù)組件名稱動態(tài)創(chuàng)建出組件的相關(guān)資料,文中通過圖文與示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-11-11