테스트 사이트 - 개발 중인 베타 버전입니다

검증 예제

· 8년 전 · 1579
검증 예제

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/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>

HTML 양식 속성 인 novalidate 는 기본 브라우저 유효성 검증을 사용 불가능하게하는 데 사용됩니다.

예제 설명
AngularJS 지시어 ng-model 은 입력 요소를 모델에 바인딩합니다.

모델 객체에는 사용자 와 전자 메일 이라는 두 가지 속성이 있습니다.

ng-show 때문에 사용자 또는 전자 메일이 $ dirty 및 $ invalid 인 경우에만 color : red가 표시됩니다 .

댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

게시글 목록

번호 제목
1296
1295
1294
1293
1292
1291
1290
1289
jQuery Mobile navigate 이벤트
1288
1287
jQuery Mobile hashchange event
1286
1285
1284
1283
1282
1281
1280
1279
1278
1277
1276
1275
1274
1273
1272
1271
1270
1269
1268
1267