「AngularJS入門」フォームの入力値のバリデーションのサンプル

書式
<form ng-app="xxx" ng-controller="validateCtrl"
name="フォーム名" novalidate>
app.controller('validateCtrl’, function($scope) {
some code
使用例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>

<h2>サンプル</h2>

<form ng-app="cftApp" ng-controller="validateCtrl" 
name="cftForm" novalidate>

<p>username:<br>
<input type="text" name="user" ng-model="user" required>
<span style="color:red" ng-show="cftForm.user.$dirty && cftForm.user.$invalid">
<span ng-show="cftForm.user.$error.required">ログインIDは必須</span>
</span>
</p>

<p>メールアドレス:<br>
<input type="email" name="email" ng-model="email" required>
<span style="color:red" ng-show="cftForm.email.$dirty && cftForm.email.$invalid">
<span ng-show="cftForm.email.$error.required">メールは必須</span>
<span ng-show="cftForm.email.$error.email">メールは無効</span>
</span>
</p>

<p>
<input type="submit"
ng-disabled="cftForm.user.$dirty && cftForm.user.$invalid ||  
cftForm.email.$dirty && cftForm.email.$invalid">
</p>

</form>

<script>
var app = angular.module('cftApp', []);
app.controller('validateCtrl', function($scope) {
    $scope.user = 'testuser';
    $scope.email = 'testuser@sample.com';
});
</script>

</body>
</html>

 

AngularJS

Posted by arkgame