2017-05-09 58 views
0

我想驗證使用ng-pattern特定域的電子郵件驗證,可以說,我想這個模式[email protected][email protected]這是兩個條件,我有當用戶的輸入值適用。我試過emailFromat以下,但它在所有情況下都顯示無效?任何更好的方法?ng-pattern不適用於特定的電子郵件地址?

main.html中

<div layout="row"> 
    <md-input-container flex="100"> 
     <label>Cc</label> 
     <input type="email" name="email" ng-model="notifyCtrl.cc" ng-list="," ng-pattern="pattern(user.type)"> 
     <div class="help-block" ng-messages="notifyForm.email.$error" ng-show="notifyForm.email.$touched && notifyForm.email.$invalid"> 
      <div ng-if="user.type === 'notify'"> 
       <div ng-message="pattern"> 
        An email name must only contain a-z, A-Z, 0-9, or _ characters.(ie. [email protected], [email protected] 
       </div> 
      </div> 
     </div> 
    </md-input-container> 
</div> 

ctrl.js

var emailFormat = "/^[a-z0-9!#$%&'*+/=?^_`{|}~.-][email protected]+(?: |[a-z0-9-]+\.com|com|[a-z0-9-])$/" 

$scope.pattern = function(type){ 
    if(type === 'notify') { 
     return emailFormat; 
    } 
}; 
+0

爲什麼它被向下投?我能否收到一些評論? – hussain

回答

0

從正則表達式刪除引號。沒有必要。 https://regex101.com/r/njkXrr/1

var emailFormat = /^[a-zA-Z]{2}[a-z0-9!#$%&'*+/=?^_`{|}~.-]*[a-zA-Z]{2}@+(?: |[a-z0-9-]+\.com|com|[a-z0-9-])$/ 

$scope.pattern = function(type){ 
    if(type === 'notify') { 
     return emailFormat; 
    } 
}; 
+0

'/'不是爲了逃避,它是爲了一個文字匹配(它工作,順便說一句)。然而,這個正則表達式仍然是壞的;它會匹配'@'後的單個字符。 (即'abc123 @ a')。 https://regex101.com/r/KcIbq3/1/ – Claies

+0

如何使它特定於像[email protected]這樣的域名? – hussain

+0

@Claies感謝隊友。更新了答案 –

相關問題