AngularJS自动表单验证

前端之家收集整理的这篇文章主要介绍了AngularJS自动表单验证前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

AngularJS的另外一种表单验证方式是自动验证,即通过directive来实现,除了AngularJS自带的directive,还需要用到angular-auto-validate这个第三方module。

有关angular-auto-validate:

  • 安装:npm i angular-auto-validate
  • 引用:
    <script src="../bower_components/ladda/dist/ladda.min.js">
    <script src="../bower_components/angular-ladda/dist/angular-ladda.min.js">

页面如下:

<div class="jb51code">
<pre class="brush:xhtml;">

<Meta charset="gb2312">

<div class="container main-content" ng-controller="myCtrl1">

<form ng-submit="onSubmit()" novalidate="novalidate">
<div class="form-group">
<label for="name" class="control-label">Name
<input type="text" class="form-control" id="name" ng-model="formModel.name" required="required"/>

<div class="form-group">
<label for="email" class="control-label">Email
<input type="email" class="form-control" id="email" ng-model="formModel.email" required="required"/>

<div class="form-group">
<label for="username" class="control-label">Username
<input type="text"
class="form-control"
id="username"
ng-model="formModel.username"
required="required"
ng-pattern="/^[A-Za-z0-9_]{1,32}$/"
ng-minlength="7"
ng-pattern-err-type="badUsername"
/>

<div class="form-group">
<label for="age" class="control-label">Age
<input type="number"
class="form-control"
id="age"
ng-model="formModel.age"
required="required"
min="18"
max="65"
ng-min-err-type="tooYoung"
ng-max-err-type="tooOld"
/>

<div class="form-group">
<label for="sex" class="control-label">Sex
<select name="sex" id="sex" class="form-control" ng-model="formModel.sex" required="required">
<option value="">Please choose
<option value="male">Mail
<option value="femail">Femail

<div class="form-group">
<label for="password" class="control-label">Password
<input type="text" class="form-control" id="password" ng-model="formModel.password" required="required" ng-minlength="6"/>

<div class="form-group">

<button class="btn btn-primary"
ladda = "submitting"
data-style="expand-right"
type="submit">
<span ng-show="submitting">正在注册...
<span ng-show="!submitting">注册

@H_403_97@
{{formModel | json}}


<script src="../node_modules/angular-auto-validate/dist/jcs-auto-validate.min.js">
<script src="../node_modules/angular-sanitize/angular-sanitize.min.js">
<script src="../node_modules/angular-localize/angular-localize.min.js">

<script src="../bower_components/ladda/dist/spin.min.js">
<script src="../bower_components/ladda/dist/ladda.min.js">
<script src="../bower_components/angular-ladda/dist/angular-ladda.min.js">

先看提交按钮:

-->