[英]Single quotes in AngularJS tag
我有以下使用ng-pattern指令的電子郵件驗證代碼。 我需要在錯誤驗證中包含單引號,以便例如:asd'f@dfs.com失敗。 我不想使用默認的angular指令,因為后續的..(點),^,逗號等不適合
<input type="email" name="username" placeholder="jasdf@asdf.com" ng-model="user.username" ng-maxlength="100" ng-model-options="{ updateOn: blur }" ng-pattern='/^(([^<>()\[\]\\.\,;:\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,}))$/' required />
<div class="error-container" ng-show="userForm.username.$dirty && userForm.username.$error">
<small class="error" ng-show="userForm.username.$error.required">
Your email is required.
</small>
<small class="error" ng-show="userForm.username.$error.pattern">
Please input a valid email.
</small>
<small class="error" ng-show="userForm.username.$error.maxlength">
Your email cannot be longer than 100 characters
</small>
從上面可以看出,我正在使用以下正則表達式: ng-pattern='/^(([^<>()\\[\\]\\\\.\\,;:\\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,}))$/'
我的問題是我如何包括單引號('),以便它與ng-pattern標簽引號以及正則表達式中的(^)不沖突。 我四處搜尋,看來我應該使用‘但不確定如何實現。 感謝任何幫助。
您可以使用以下簡短版本:
ng-pattern="/^(([^<>()\[\]\\.,;:\s@^\x22\x27]+(\.[^<>()\[\]\\.,;^:\s@\x22\x27]+)*)|(\x22[^@]+\x22))@((\[[0-9]{1,3}(\.[0-9]{1,3}){3}])|(([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}))$/"
這是正則表達式演示
更改為:
\\.[0-9]{1,3}
收縮,因為它重復了3次到(\\.[0-9]{1,3}){3}
\\x27
( '
)和\\x22
( "
)符號 ^
添加到第一個否定字符類中 \\x22.+\\x22
會變成\\x22[^@]+\\x22
這樣我們就不會溢出到域部分而留在用戶名部分內。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.