2015-08-19 11 views
1

我使用以下NG模式,用於驗證電子郵件在AngualurJs中使用ng-pattern進行電子郵件驗證時出錯?

"/^(([^<>()[\]\\.,;:\[email protected]\"]+(\.[^<>()[\]\\.,;:\[email protected]\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/" /> 

我的電子郵件字段代碼如下:

<input type="text" id="email" name="email" data-ng-model="userInfoDto.email" class="form-control" placeholder="Email" title="Email" data-ng-required="true autofocus="autofocus" data-ng-blur="chcekUniqueEmail()" data-ng-pattern="/^(([^<>()[]\\.,;:\[email protected]\"]+(\.[^<>()[]\\.,;:\[email protected]\"]+)*)|(\".+\"))@‌​(([[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z‌​]{2,}))$/"/

但我得到以下錯誤

  • 無效的位置()中的文本(] +(。[^))
  • 開始標記()未正確關閉,預期'>'。

請任何建議。

+0

請添加一些代碼調試 –

+0

()[\] \\。,;:\ s @ \ 「] +(\ [^ <>()[\] \\;:。\ S @ \」] +)| 「+ \」 *)(\))@((\ [[0-9 ] {1,3} \ [0-9] {1,3} \ [0-9] {1,3} \ [0-9] {1,3} \])|。。。((一個-zA-Z \ -0-9] + \。)+ [a-zA-Z] {2,}))$ /「/> –

+0

爲什麼不能使用?而不是」 –

回答

1

如果要驗證電子郵件,請使用type =「email」而不是type =「text」的輸入。 AngularJS具有開箱即用的電子郵件驗證功能,因此無需爲此使用ng-pattern。

這裏是原始文件的例子:

<script> 
function Ctrl($scope) { 
    $scope.text = '[email protected]'; 
    $scope.email = /^[a-z]+[a-z0-9._][email protected][a-z]+\.[a-z.]{2,5}$/; 
} 
</script> 
<form name="myForm" ng-controller="Ctrl"> 
    Email: <input type="email" name="input" ng-model="text" required> 
    <span class="error" ng-show="myForm.input.$error.required"> Required!</span> 
    <span class="error" ng-show="myForm.input.$error.email"> Not valid email!</span> 
    <tt>text = {{text}}</tt><br/> 
    <tt>myForm.input.$valid = {{myForm.input.$valid}}</tt><br/> 
    <tt>myForm.input.$error = {{myForm.input.$error}}</tt><br/> 
    <tt>myForm.$valid = {{myForm.$valid}}</tt><br/> 
    <tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br/> 
    <tt>myForm.$error.email = {{!!myForm.$error.email}}</tt><br/> 
</form> 

詳情閱讀本文檔:https://docs.angularjs.org/api/ng/input/input%5Bemail%5D

+0

嗨Ganesh,謝謝你的回覆,但是這個AngularJS的默認行爲電子郵件字段在以下測試用例中失敗1).email @ domain.com 2)email。@ domain.com 3)email .. [email protected] 4)email @ domain 5)[email protected] 6)[email protected]所以我使用這種模式 –

+0

嗨Ganesh,謝謝你的回覆,但是這個AngularJS的默認行爲電子郵件字段在以下測試用例失敗1).email @ domain.com 2)email。@ domain.com 3)email..email @ domain.com 4)email @ domain 5)[email protected] 6)[email protected]讓我使用此模式/ ^(([[<(()[] \\。,;:\ s @ \「] +(\。[^​​ <>()[] \\。,;:\ s @ \」] +)*)| 「+ \」。(\))@(([0-9] {1,3} \ [0-9] {1,3} \ [0-9] { 1,3} \ [0-9] {1,3}])|(([A-ZA-Z \ - 0-9] + \)+ [A-ZA-Z] {2,} ))$/ –

+0

檢查此https://docs.angularjs.org/guide/forms#modifying-built-in-validators的腳本代碼 –

相關問題