AngularJS表單驗證

來源:互聯網
上載者:User

標籤:erro   UI   https   utf-8   body   required   angularjs   .com   列印   

AngularJS的表單驗證大致有兩種,一種是手動驗證,一種是自動驗證。

手動驗證:

是通過AngularJS表單的屬性來驗證。而成為AngularJS表單必須滿足兩個條件:1、給form元素加上novalidate="novalidate";2、給form元素加上name="theForm".

<!DOCTYPE html><html><head lang="en">    <meta charset="UTF-8">    <title></title>    <script src="https://cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script></head><body>    <form ng-app="myApp" ng-controller="validateCtrl" name="myForm" novalidate="novalidate">        <p>            使用者名稱            <input type="text" name="user" ng-model="user" required/>            <span style="color:red" ng-show="myForm.user.$error.required">必填項</span>        </p>    </form></body><script>    var app = angular.module(‘myApp‘,[]);    app.controller(‘validateCtrl‘,function($scope){        $scope.user = ‘dddddd‘    })</script></html>

  

● 給form加上novalidate="novalidate"意味著表單將不再使用HTML5驗證特性
● 給form加上name="theForm"意味著表單的名稱是theForm。如何使用theForm,比如我們驗證表單是否被修改過theForm.$submitted
● 通過ng-submit提交表單
● formModel是$scope中的一個屬性
● 對錶單的Email進行了手動驗證,使用了AngularJS表單的眾多屬性,比如theForm.email.$valid,theForm.$pristine,theForm.$submitted, theForm.email.$error.required,theForm.email.$error.email
● 通過<pre>{{theForm | json}}</pre>把AngularJS表單的所有屬性都列印出來

 

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.