2017-02-24 61 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}$/; 

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