最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于angularJS的表單驗(yàn)證指令介紹

 更新時(shí)間:2016年10月21日 09:57:26   投稿:jingxian  
下面小編就為大家?guī)硪黄赼ngularJS的表單驗(yàn)證指令介紹。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

做開發(fā)這么久一直被表單驗(yàn)證這個(gè)看似簡單又不簡單的東西搞得非常煩躁,于是寫了個(gè)表單驗(yàn)證的框架,再也不用擔(dān)心這個(gè)心煩的驗(yàn)證了。

這里使用AngularJS的指令進(jìn)行處理代碼及其簡潔明了

下面是指令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+'-error'));
              break;
            }else {
              hidError(that,that.attr(attr.name+'-error'));
            }
          }else if(attr.name=='cc-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=='cc-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('[cc-submit]').click(function (e) {
        first=false;
        clearInterval(checkInterval);
        checkForm(element);
      });
      //預(yù)檢測時(shí)不顯示錯(cuò)誤提示
      checkForm(element);
    }
  }
}]);

對應(yīng)的需要一點(diǎn)點(diǎn)CSS代碼,下面是用LESS寫的

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

對應(yīng)的CSS就是

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

用法1

<div>
  <h1>test</h1>
  <form cc-form="form1">
    <div class="from-group">
      <input type="password" ng-model="user.password" cc-max="16" cc-max-error="1" cc-min="6" cc-min-error="2">
      <div class="help-block">
        <p cc-error-1>密碼長度不能超過16位</p>
        <p cc-error-2>密碼最短為6</p>
      </div>
    </div>
    <div class="form-group">
      <input ng-model="user.phone" cc-regex="\d{11}" cc-regex-error="1">
      <div class="help-block">
        <p cc-error-1>請輸入正確的手機(jī)號(hào)</p>
      </div>
    </div>
    <div class="form-group">
      <input ng-model="user.fax" cc-require="1">
      <div class="help-block">
        <p cc-error-1>請輸入傳真</p>
      </div>
    </div>
    <div class="form-group">
      <input ng-model="user.email" cc-email="1">
      <div class="help-block">
        <p cc-error-1>請輸入正確的郵箱</p>
      </div>
    </div>
    <button type="button" cc-submit>提交</button>
  </form>
</div>

用法2

  <form class="form shadow-box" cc-form="form1">
    <h3>登錄</h3>
    <div class="form-group">
      <div class="cellphone">
        <input placeholder="手機(jī)號(hào)" ng-model="user.loginName" name="loginName" cc-regex="\d{11}" cc-regex-error="1" cc-require="2"/>
      </div>
    </div>
    <div class="help-block">
      <p cc-error-1>請輸入正確的手機(jī)號(hào)</p>
      <p cc-error-2>請輸入手機(jī)號(hào)</p>
    </div>
    <div class="form-group">
      <div class="password">
        <input placeholder="密碼" type="password" ng-model="user.password" name="password" cc-require="1"/>
      </div>
    </div>
    <div class="help-block">
      <p cc-error-1>請輸入密碼</p>
    </div>
    <div class="form-group">
      <div class="vcode">
        <input placeholder="驗(yàn)證碼" ng-model="user.vcode" name="vcode" cc-require="1">
        <img src="">
      </div>
    </div>
    <div class="help-block">
      <p cc-error-1>請輸入驗(yàn)證碼</p>
    </div>
    <div class="checkbox">
      <label>
        <input type="checkbox" ng-model="user.rememberPass" ng-true-value="true" ng-false-value="false">自動(dòng)登錄
      </label>
    </div>
    <p class="to-register">沒有賬號(hào)?<a href="#/main/register">立即注冊</a></p>
    <button type="button" ng-click="login(1)" cc-submit>登錄</button>
  </form>

如此簡潔明了

以上就是小編為大家?guī)淼幕赼ngularJS的表單驗(yàn)證指令介紹全部內(nèi)容了,希望大家多多支持腳本之家~

相關(guān)文章

  • AngularJS的$location使用方法詳解

    AngularJS的$location使用方法詳解

    這篇文章主要介紹了AngularJS的$location使用方法詳解的相關(guān)資料,希望通過本文大家能夠掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10
  • AngularJS constant和value區(qū)別詳解

    AngularJS constant和value區(qū)別詳解

    angularJS可以通過constant(name,value)和value(name,value)對于創(chuàng)建服務(wù)也是很重要的。他們之間有什么不同呢?今天小編給大家分享AngularJS constant和value區(qū)別詳解,需要的朋友參考下
    2017-02-02
  • AngularJS 在同一個(gè)界面啟動(dòng)多個(gè)ng-app應(yīng)用模塊詳解

    AngularJS 在同一個(gè)界面啟動(dòng)多個(gè)ng-app應(yīng)用模塊詳解

    這篇文章主要介紹了AngularJS 在同一個(gè)界面啟動(dòng)多個(gè)ng-app應(yīng)用模塊詳解的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • AngularJS 過濾與排序詳解及實(shí)例代碼

    AngularJS 過濾與排序詳解及實(shí)例代碼

    這篇文章主要介紹了AngularJS 過濾與排序,這里整理了詳細(xì)的資料及簡單實(shí)例代碼,有需要的小伙伴可以參考下
    2016-09-09
  • Angular使用ng-messages與PHP進(jìn)行表單數(shù)據(jù)驗(yàn)證

    Angular使用ng-messages與PHP進(jìn)行表單數(shù)據(jù)驗(yàn)證

    這篇文章主要介紹了Angular使用ng-messages與PHP進(jìn)行表單數(shù)據(jù)驗(yàn)證,ng-messages提供了更方便的表單數(shù)據(jù)驗(yàn)證服務(wù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • angular + express 實(shí)現(xiàn)websocket通信

    angular + express 實(shí)現(xiàn)websocket通信

    最近需要實(shí)現(xiàn)一個(gè)功能,后端通過TCP協(xié)議連接雷達(dá)硬件的控制器,前端通過websocket連接后端,當(dāng)控制器觸發(fā)消息的時(shí)候,把信息通知給所以前端,本文給的大家講解angular + express 實(shí)現(xiàn)websocket通信的思路,感興趣的朋友一起看看吧
    2023-09-09
  • Angular模板表單校驗(yàn)方法詳解

    Angular模板表單校驗(yàn)方法詳解

    這篇文章主要為大家詳細(xì)介紹了Angular模板表單校驗(yàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • 利用Angular+Angular-Ui實(shí)現(xiàn)分頁(代碼加簡單)

    利用Angular+Angular-Ui實(shí)現(xiàn)分頁(代碼加簡單)

    這篇文章主要介紹了利用Angular+Angular-Ui實(shí)現(xiàn)分頁,利用Angular+Angular-Ui實(shí)現(xiàn)的分頁分頁代碼更加簡單,更加容易懂哦,相信本文的內(nèi)容對大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。
    2017-03-03
  • Angular.js實(shí)現(xiàn)動(dòng)態(tài)加載組件詳解

    Angular.js實(shí)現(xiàn)動(dòng)態(tài)加載組件詳解

    這篇文章主要給大家介紹了關(guān)于Angular.js實(shí)現(xiàn)動(dòng)態(tài)加載組件的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。
    2017-05-05
  • AngularJS頁面帶參跳轉(zhuǎn)及參數(shù)解析操作示例

    AngularJS頁面帶參跳轉(zhuǎn)及參數(shù)解析操作示例

    這篇文章主要介紹了AngularJS頁面帶參跳轉(zhuǎn)及參數(shù)解析操作,結(jié)合具體實(shí)例形式分析了AngularJS使用URL傳遞參數(shù)及參數(shù)的接收、解析等相關(guān)操作技巧,需要的朋友可以參考下
    2017-06-06

最新評論

吉安市| 日喀则市| 岳池县| 元阳县| 临城县| 河北省| 河北区| 玉树县| 宁乡县| 海安县| 祁连县| 岗巴县| 内乡县| 清流县| 乌鲁木齐市| 东平县| 许昌县| 新营市| 丰顺县| 临清市| 宣汉县| 滦南县| 津市市| 米泉市| 华安县| 井研县| 光泽县| 习水县| 乌兰浩特市| 高阳县| 望城县| 紫阳县| 土默特左旗| 双牌县| 女性| 公安县| 和静县| 达拉特旗| 青神县| 霍山县| 湘乡市|