Home  >  Article  >  php教程  >  Detailed explanation of Angular form validation examples

Detailed explanation of Angular form validation examples

高洛峰
高洛峰Original
2016-12-09 14:53:401038browse

Form verification

I'll go. I feel like I'm actually a very stupid person. I always waste a long time just because I misspelled a word or something like that. This really doesn't work. I need to treat this correctly. Question, okay, let’s get to the point. Angular also has form validations such as minlength, maxlength, and required. It also supports h5 validations. The h5 validations are type, type='email', number, url. Yeah, these, and now we need to use angular to verify, you can define the style, good, then how to verify, good code

<!DOCTYPE html>
<html ng-app=&#39;app&#39;>
<head>
<meta charset=&#39;UTF-8&#39;>
<title>form1</title>
<link rel="stylesheet" type="text/css" href="static/plugins/bootstrap.min.css">
<script type="text/javascript" src=&#39;static/plugins/angular.min.js&#39;></script>
<script type="text/javascript" src=&#39;static/plugins/angular-messages.js&#39;></script>
</head>
<body>
<div class="col-md-6">
<form role="form" name="myForm" class="form-horizontal" novalidate>
<label>用户名</label>
<input type="email" placeholder="ng-Messages测试" name="name" ng-model="username.name"
ng-minlength=3 ng-maxlength=20 required />
<div ng-messages="myForm.name.$error">
<div ng-message="required">必填项</div>
<div ng-message="minlength">字符太短小于3</div>
<div ng-message="maxlength">字符太长大于20</div>
<div ng-message=&#39;email&#39;>正确的邮箱格式</div>
</div>
</form>
</div>
</body>
<script type="text/javascript">
angular.module("app", [&#39;ngMessages&#39;]);
</script>
</html>

That’s it, there are a few points to declare,

 The first is to introduce angular-messages.js,

The second is that messages and messages must be seen clearly,

The third myForm.name.$error: This myForm is the name value of the form, and name is the input to be verified name value.

The fourth thing is that you can also customize the verification.

 Well, the fifth words have not been revealed yet. The masters can take a look and see if there is anything else to pay attention to. Welcome to point out.

Let’s talk about how to do custom verification, upload the code

<input type="text"
placeholder="Desired username"
name="username"
ng-model="signup.username"
ng-minlength=3
ng-maxlength=20
ensure-unique="username" required />

You see, this ensure-unique is a custom verification, it needs to be unique. This is how it is written in html, customized js I wrote the code myself. There is also a corresponding code here, which is written with instructions. Thank you for the code

angular.module(&#39;myApp&#39;, [])
.directive(&#39;ensureUnique&#39;, [&#39;$http&#39;, function($http) {
return {
require: &#39;ngModel&#39;,
link: function(scope, ele, attrs, c) {
scope.$watch(attrs.ngModel, function() {
$http({
method: &#39;POST&#39;,
url: &#39;/api/check/&#39; + attrs.ensureUnique,
data: {&#39;field&#39;: attrs.ensureUnique}
}).success(function(data, status, headers, cfg) {
c.$setValidity(&#39;unique&#39;, data.isUnique);
}).error(function(data, status, headers, cfg) {
c.$setValidity(&#39;unique&#39;, false);
});
});
}
};
}]);

Did you see that the custom verification is completed in two steps of html and js? It is custom verification. I feel I'm a bit low-key, so I can't do it. Anyway, I posted these two pieces of code to tell you how to write custom validation, haha

Yeah, and there are also the form validation properties of anglar. Anyway, just upload the form and it will be clear at a glance. These are all available on Cainiao.com , you can search for it

Detailed explanation of Angular form validation examples

Then, how to use this, okay, here is the code

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<h2>验证实例</h2>
<form ng-app="myApp" ng-controller="validateCtrl"
name="myForm" novalidate>
<p>用户名:<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">用户名是必须的。</span>
</span>
</p>
<p>邮箱:<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">邮箱是必须的。</span>
<span ng-show="myForm.email.$error.email">非法的邮箱地址。</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(&#39;myApp&#39;, []);
app.controller(&#39;validateCtrl&#39;, function($scope) {
$scope.user = &#39;John Doe&#39;;
$scope.email = &#39;john.doe@gmail.com&#39;;
});
</script>
</body>
</html>

A few points on how to use this,

 1. As long as you use one angular.js, you don’t need to introduce anything else js, but you know what the disadvantage is. It cannot verify more conditions. Hey, it depends on your needs.

  2. She uses ng-show to display it. Where do the myForm.user.$error.required here come from? They are still the same as above. They are all name values. This is it. You See required, it corresponds to $dirty so....myForm.user.$dirty, haha, the meanings of these representatives are all in the form. I feel that this kind of verification condition is effective. Again, it depends on your needs.

  3. There is another problem with this usage. For example, if you want to verify a required first, if there is no content in the input box at the beginning, its verification prompt will not appear, so you have to give it js first. It defaults to a value. I feel that this effect is not good. So you see $scope.user = 'John Doe';js on the page assigns a value to him first.

  4. There is another question. You have to assign a value first, and then you have to get a controller, and you have to define a controller. I think in summary, I still think the first method is better.

  5. There is another question. Remember to bind ng-model to each Input, otherwise how to monitor it. Personal opinion,

Also, these two can actually be combined. If not, you can look at the code. Okay, let’s get into the code

<!DOCTYPE html>
<html ng-app=&#39;app&#39;>
<head>
<meta charset=&#39;UTF-8&#39;>
<title>form1</title>
<link rel="stylesheet" type="text/css" href="static/plugins/bootstrap.min.css">
<script type="text/javascript" src=&#39;static/plugins/angular.min.js&#39;></script>
<script type="text/javascript" src=&#39;static/plugins/angular-messages.js&#39;></script>
</head>
<body ng-controller=&#39;ctrl&#39;>
<div class="col-md-6">
<form role="form" name="myForm" class="form-horizontal" novalidate>
<label>用户名</label>
<input type="email" placeholder="ng-Messages测试" name="name" ng-model="username.name"
ng-minlength=3 ng-maxlength=20 required />
<div ng-messages="myForm.name.$error">
<div ng-message="required">必填项</div>
<div ng-message="minlength">字符太短小于3</div>
<div ng-message="maxlength">字符太长大于20</div>
<div ng-message=&#39;email&#39;>正确的邮箱格式</div>
</div>
名字 <input type=&#39;text&#39; name=&#39;name1&#39; ng-model=&#39;name1&#39; required>
<span style=&#39;color:red&#39; ng-show=&#39;myForm.name1.$dirty&#39;>
<span ng-show=&#39;myForm.name1.$error.required&#39;>名字是必须的</span>
</span>
</form>
</div>
</body>
<script type="text/javascript">
var app=angular.module("app", [&#39;ngMessages&#39;]);
app.controller(&#39;ctrl&#39;,function($scope){
$scope.name1=&#39;wenwen&#39;;
})
</script>
</html>

That’s it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn