관리 메뉴

nalaolla

[AngularJS] 13. 검사(Validations) - Angular JS 강좌 본문

AngularJS_1

[AngularJS] 13. 검사(Validations) - Angular JS 강좌

날아올라↗↗ 2016. 6. 14. 13:33
728x90

AngularJS Input Validation

 AngularJS 서식과 제어는 입력 데이터를 검사 할 수 있습니다.
 


 1. Input validation
  이전 장에서 폼과 제어에 대해 배웠습니다.

  AngularJS 폼과 제어는 유효성 서비스를 제공하고, 사용자의 무효한 입력을 알립니다.


  * tip: Client-Side 유효성 검사는 사용자 입력을 단일로 보안화 하지 않습니다. Server side 유효성 검사 또한 필요합니다.








 2. Application Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 
<!DOCTYPE html>
<html>
<script src"http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>  
<body>
 
    <h2>Validation Example</h2>
 
    <form ng-app="myApp" ng-controller="validateCtrl" 
    name="myForm" novalidate>
 
        <p>Username:<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">Username is required.</span>
        </span>
        </p>
 
        <p>Email:<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">Email is required.</span>
        <span ng-show="myForm.email.$error.email">Invalid email address.</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>
 
cs




 * tip: HTML 폼 속성 novalidate는 기본 브라우저 유효성검사를 쓰지않게 하기 위함입니다.

 








 3. Example Explained
  AngularJS ng-model 지시어는 모델로부터의 입력 요소를 연결합니다.

  모델 객체는 두 개의 속성을 갖습니다: user와 email

  ng-show 때문에, user나 email이 $dirty와 $invalid 할 때, spans 태그가 빨간 글씨를 보이게 됩니다.

PropertyDescription
$dirtyThe user has interacted with the field.
$validThe field content is valid.
$invalidThe field content is invalid.
$pristineUser has not interacted with the field yet.




 * 위 강좌는 W3Schools 를 참고하여 작성하였습니다.


728x90