2017-02-24 66 views
0

我正在關注official angular documentation,但它不適用於我。什麼可能是錯的?我需要驗證是否需要手機,並遵循國際標準如+4400000000(加上信號和數字,沒有空格,只有加號後的數字)。在Regular驗證表單字段與regEx

我的代碼....

.controller('ModalInstanceChangeDetailsCtrl', function ($scope, $rootScope, $uibModalInstance, items) { 
    $scope.rxMobile = '^\+[1-9]{1}[0-9]{3,14}$'; 
} 

<form name="form"> 
<div class="form-group"> 
     <label>Mobile number</label><label class="fieldError" ng-show="form.mobile.$error.required || !form.mobile.$valid">This field is required and must use International format, ex: +440000000000</label> 
     <input ng-pattern="rxMobile" required ng-model="mobile" name="mobile" type="text" class="form-control"> 
</div> 
</form> 

什麼建議嗎?

回答

0

使用本正則表達式驗證國際標準電話號碼:

$scope.rxMobile = '^\\+\\d{10}\\b';

+0

SyntaxError:無效正則表達式:/ ^^ + d {10} $ /:無法在新的RegExp處重複 --- $ scope.rxMobile =「^ \ + \ d \ d {10} \ b」 ; –

+0

@MarcoJr檢查編輯 – m87

0

使用正則表達式來驗證電話號碼

+[\d]{10}$

0

好吧,我發現了什麼問題?

$scope.rxMobile = /^\+[1-9]{1}[0-9]{3,14}$/; 

如果我沒有引用它,它是有效的。