Angularjs Quick Start Guide 14: Data validation

Source: Internet
Author: User

Angularjs forms and controls can validate data entered by the user.

Input validation

In the previous chapters you've learned about Angularjs forms and controls.

Angularjs forms and controls can provide data validation services and prompt information about illegal data.

Please note that in no case can the client-side data be verified, and the data validation of the server is also required.
Program code
<!DOCTYPE HTML><HTML><Scriptsrc= "Http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></Script><Body><H2>Validation Example</H2><formNg-app= "MYAPP"Ng-controller= "Validatectrl"name= "MyForm"novalidate><P>Username:<BR>  <inputtype= "text"name= "User"Ng-model= "User"Required>  <spanstyle= "Color:red"Ng-show= "Myform.user. $dirty && myform.user $invalid">  <spanNg-show= "Myform.user. $error. Required">Username is required.</span>  </span></P><P>Email:<BR>  <inputtype= "Email"name= "Email"Ng-model= "Email"Required>  <spanstyle= "Color:red"Ng-show= "Myform.email. $dirty && myform.email $invalid">  <spanNg-show= "Myform.email. $error. Required">Email is required.</span>  <spanNg-show= "Myform.email. $error. Email">Invalid email address.</span>  </span></P><P>  <inputtype= "Submit"ng-disabled= "Myform.user. $dirty && myform.user $invalid | | Myform.email. $dirty && myform.email. $invalid "></P></form><Script>varapp=Angular.module ('myApp', []); App.controller ('Validatectrl', function($scope) {$scope. User= 'John Doe'; $scope. Email= '[email protected]';});</Script></Body></HTML>

Run

The novalidate property indicates that the default browser data validation feature is disabled.
Code explanation

The ANGULARJS directive Ng-model binds the input tag to the model.

The model object has two properties:user and email.

By using the ng-show directive to show or hide the <span> element, the font of the element is set to red, which is displayed when user or email is $dirty and $invalid .

Filters
FilterDescription
$dirty Indicates that the contents of the current field have been modified.
$valid Indicates that the content in the current field is valid.
$invalid Indicates that the content in the current field is not valid.
$pristine Indicates that the contents of the current field have not been modified.
Previous chapter-Angularjs Quick Start Guide 13: Next chapter of the form-angularjs Quick Start Guide 15:api

Angularjs Quick Start Guide 14: Data validation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.