AngularJS 輸入驗證

來源:互聯網
上載者:User

標籤:

AngularJS 表單和控制項可以提供驗證功能,並對使用者輸入的非法資料進行警告。

注意:用戶端的驗證不能確保使用者輸入資料的安全,所以服務端的資料驗證也是必須的。

<!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>驗證執行個體</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 = ‘[email protected]‘;            });        </script>    </body></html>

HTML 表單屬性 novalidate 用于禁用瀏覽器預設的驗證。

執行個體解析

AngularJS ng-model 指令用於綁定輸入元素到模型中。

模型對象有兩個屬性: user 和 email。

我們使用了 ng-show指令, color:red 在郵件是 $dirty 或 $invalid 才顯示。

http://www.cnblogs.com/chrisghb8812/p/5643470.html

屬性 描述
$dirty 表單有填寫記錄
$valid 欄位內容合法的
$invalid 欄位內容是非法的
$pristine 表單沒有填寫記錄

AngularJS 輸入驗證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.