基于angularJS的表单验证指令介绍

网络编程 2021-07-04 20:01www.168986.cn编程入门
狼蚁网站SEO优化长沙网络推广就为大家带来一篇基于angularJS的表单验证指令介绍。长沙网络推广觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随长沙网络推广过来看看吧

做开发这么久一直被表单验证这个看似简单又不简单的东西搞得非常烦躁,于是写了个表单验证的框架,再也不用担心这个心烦的验证了。

这里使用AngularJS的指令进行处理代码及其简洁明了

狼蚁网站SEO优化是指令JS代码

app.directive('Form',['$parse',function ($parse) {
  return {
    restrict:'A',
    link:function (scope,element,attrs) {
      var first=true;
      var errors=0;
      var checkInterval;
      function showError(input,errorIndex) {
        if(first){
          errors++;
          $parse(element.attr('-form')).assign(scope,false);
          refreshScope(scope);
          return;
        }
        input.addClass('hasError');
        input.closest('.form-group').addClass('hasError');
        input.next('.help-block').find('.-show').removeClass('-show');
        input.next('.help-block').find('[-error-'+errorIndex+']').addClass('-show');
        input.closest('.form-group').next('.help-block').find('.-show').removeClass('-show');
        input.closest('.form-group').next('.help-block').find('[-error-'+errorIndex+']').addClass('-show');
      }
      function hidError(input,errorIndex) {
        errors--;
        if(errors==0){
          $parse(element.attr('-form')).assign(scope,true);
          refreshScope(scope);
        }
        input.removeClass('hasError');
        input.closest('.form-group').removeClass('hasError');
        input.next('.help-block').find('[-error-'+errorIndex+']').removeClass('-show');
        input.closest('.form-group').next('.help-block').find('[-error-'+errorIndex+']').removeClass('-show');
      }
      function checkInput(input) {
        var that=$(input);
        // $('[-email]')[0].attributes[0].name
        var attrs=input.attributes;
        var value=that.val();
        for(var i=0,attr;attr=attrs[i];i++){
          if(attr.name=='-email'){
            if(/^[\w-]+(\.[\w-]+)@[\w-]+(\.[\w-]+)+$/.test(value)){
              hidError(that,attr.value)
            }else{
              showError(that,attr.value);
              break;
            }
          }else if(attr.name=='-phone'){
            if(/\d{11}/.test(value)){
              hidError(that,attr.value);
            }else {
              showError(that,attr.value);
              break;
            }
          }else if(attr.name=='-max'){
            if(value.length>attr.value){
              showError(that,that.attr(attr.name+'-error'));
              break;
            }else {
              hidError(that,that.attr(attr.name+'-error'));
            }
          }else if(attr.name=='-min'){
            if(value.length<attr.value){
              showError(that,that.attr(attr.name+'-error'));
              break;
            }else {
              hidError(that,that.attr(attr.name+'-error'));
            }
          }else if(attr.name=='-regex'){
            var patten=new RegExp(attr.value);
            if(patten.test(value)){
              hidError(that,that.attr(attr.name+'-error'));
            }else {
              showError(that,that.attr(attr.name+'-error'));
              break;
            }
          }else if(attr.name=='-require'){
            if(!value||value.trim()==''){
              showError(that,attr.value);
              break;
            }else {
              hidError(that,attr.value);
            }
          }
        }
      }
      function checkForm(form) {
        form.find('input,textarea').each(function () {
          checkInput(this);
        });
      }
      element.find('input,textarea').focus(function (e) {
        var that=this;
        first=false;
        checkInterval=setInterval(function () {
          checkInput(that);
        },500);
      });
      element.find('input,textarea').blur(function (e) {
        first=false;
        checkInput(this);
        clearInterval(checkInterval);
      });
      element.find('[-submit]').click(function (e) {
        first=false;
        clearInterval(checkInterval);
        checkForm(element);
      });
      //预检测时不显示错误提示
      checkForm(element);
    }
  }
}]);

对应的需要一点点CSS代码,狼蚁网站SEO优化是用LESS写的

.help-block {
  {
  &:not(.-show) {
   display: none;
  }
 }
}

对应的CSS就是

.help-block :not(.-show) {
  display: none;
}

用法1

<div>
  <h1>test</h1>
  <form -form="form1">
    <div class="from-group">
      <input type="password" ng-model="user.password" -max="16" -max-error="1" -min="6" -min-error="2">
      <div class="help-block">
        <p -error-1>密码长度不能超过16位</p>
        <p -error-2>密码最短为6</p>
      </div>
    </div>
    <div class="form-group">
      <input ng-model="user.phone" -regex="\d{11}" -regex-error="1">
      <div class="help-block">
        <p -error-1>请输入正确的手机号</p>
      </div>
    </div>
    <div class="form-group">
      <input ng-model="user.fax" -require="1">
      <div class="help-block">
        <p -error-1>请输入传真</p>
      </div>
    </div>
    <div class="form-group">
      <input ng-model="user.email" -email="1">
      <div class="help-block">
        <p -error-1>请输入正确的邮箱</p>
      </div>
    </div>
    <button type="button" -submit>提交</button>
  </form>
</div>

用法2

  <form class="form shadow-box" -form="form1">
    <h3>登录</h3>
    <div class="form-group">
      <div class="cellphone">
        <input placeholder="手机号" ng-model="user.loginName" name="loginName" -regex="\d{11}" -regex-error="1" -require="2"/>
      </div>
    </div>
    <div class="help-block">
      <p -error-1>请输入正确的手机号</p>
      <p -error-2>请输入手机号</p>
    </div>
    <div class="form-group">
      <div class="password">
        <input placeholder="密码" type="password" ng-model="user.password" name="password" -require="1"/>
      </div>
    </div>
    <div class="help-block">
      <p -error-1>请输入密码</p>
    </div>
    <div class="form-group">
      <div class="vcode">
        <input placeholder="验证码" ng-model="user.vcode" name="vcode" -require="1">
        <img src="">
      </div>
    </div>
    <div class="help-block">
      <p -error-1>请输入验证码</p>
    </div>
    <div class="checkbox">
      <label>
        <input type="checkbox" ng-model="user.rememberPass" ng-true-value="true" ng-false-value="false">自动登录
      </label>
    </div>
    <p class="to-register">没有账号?<a href="#/main/register">立即注册</a></p>
    <button type="button" ng-click="login(1)" -submit>登录</button>
  </form>

如此简洁明了

以上就是长沙网络推广为大家带来的基于angularJS的表单验证指令介绍全部内容了,希望大家多多支持狼蚁SEO~

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by