2015-09-06 87 views
1

我正在使用ngTagsInput Angular插件獲取多個電子郵件ID。下面是我的代碼:ngTagsInput如何顯示不同情況下的驗證消息?

<form name="contact_us" role="form" novalidate enctype="multipart/form-data"> 
    <div class="form-group"> 
    <label class="control-label" for="from_email"> 
     From 
    </label> 
    <tags-input ng-model="contactUs.emails" type="email" id="from_email" 
     placeholder="From" name="from_email" 
     allowed-tags-pattern="^[A-Za-z0-9._%+-][email protected](?:[A-Za-z0-9-]+.)+[A-Za-z]{2,}" 
     allow-leftover-text="false" ng-required="true" add-on-space="true"> 
    </tags-input> 
    <p class="help-block" style="color:red" 
     ng-show="contact_us.from_email.$invalid && (contact_us.$submitted || contact_us.$dirty)"> 
     Please enter proper email address 
    </p> 
    <button type="submit" class="btn btn-default" ng-click="Send(contact_us)"> 
     Send 
    </button> 
</form> 

在上面的代碼3驗證已添加那些如下:

  1. 強制字段。
  2. 該字段只能接受電子郵件ID。
  3. 它不應該允許重複的電子郵件ID。

上述案例工作正常。但是,我想根據上述發生的情況動態顯示錯誤消息。請幫助我!

回答

0

對於要求:

<p class="help-block" style="color:red" 
    ng-show="contact_us.from_email.$error.required">   
    email address is required 
</p> 

對於模式&重複,我覺得已經沒有提供有效標誌,你必須寫自己進行驗證。

重複,也許這會有所幫助。 Angularjs - How to check for unique inputs and if there is a duplicate mark both as invalid

+0

ngTagsInput插件不支持NG-change.Please讓我知道,這是任何其他方式來顯示不同驗證方法 – manisankar

1

ngTagsInput支持下面的變化捕捉屬性,將其添加到模型之前觸發

on-tag-adding="foo($tag)" 

$scope.foo(function(tag){ 
    // look for error 
    // if found return false 

    // change the text of tag 
    tag.text='what ever'; 
    return tag; 

}) 
相關問題