youyouzouzou 2016-12-01
<form name="myForm" ng-submit="addForm()" novalidate>
<div class="form-group">
<label>名称</label>
<input type="text" name="userName" placeHolder="名称" ng-minlength="3"
ng-maxlength="20" required>
<div class="m-t-xs" ng-show="myForm.userName.$invalid && myForm.submitted">
<small class="text-danger" ng-show="myForm.userName.$error.required">
请输入
</small>
<small class="text-danger" ng-show="myForm.userName.$error.minlength">
名称最小长度为3
</small>
<small class="text-danger" ng-show="myForm.userName.$error.maxlength">
名称最大长度为20
</small>
</div>
</div>
<div class="form-group">
<button type="submit">保存</button>
</div>
</form>
$scope.addForm = function(){
if($scope.myForm.$valid){
//提交表单
}else{
$scope.myForm.submitted = true;//显示错误提示信息
}
}
angularJS中提供了许多的验证指令,可以轻松的实现验证,只需要在表单元素上添加相应的ng属性,常见的如下所示:。<input Type="text" ng-model="" [name="&qu
验证表单输入的文本长度是否大于某个最小值,在输入字段上使用指令ng-minleng= "{number}":。<input type="text" ng-pattern="/[a-zA-Z]/&quo
验证表单输入的文本长度是否大于某个最小值,在输入字段上使用指令ng-minleng= "{number}":。<input type="text" ng-pattern="/[a-zA-Z]/&quo