首頁  >  文章  >  php教程  >  基於angularJS的表單驗證指令介紹

基於angularJS的表單驗證指令介紹

高洛峰
高洛峰原創
2016-12-09 14:12:301091瀏覽

做開發這麼久一直被表單驗證這個看似簡單又不簡單的東西搞得非常煩躁,於是寫了個表單驗證的框架,再也不用擔心這個心煩的驗證了。

這裡使用AngularJS的指令進行處理代碼及其簡潔明了

下面是指令JS代碼

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

   

對應的需要一點點CSS代碼,下面是用下面寫的對應詞的CSS是

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

   

用法1

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

   

用2

rree

   

2

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn