【问题标题】:Validating form field in Angular with regEx [duplicate]使用正则表达式验证 Angular 中的表单字段 [重复]
【发布时间】:2017-02-24 21:44:14
【问题描述】:

我正在关注 official angular documentation ,但它对我不起作用。有什么问题?我需要验证是否需要手机并遵循 +4400000000 等国际标准(加上信号和数字,没有空格,只有加号后的数字)。

我的代码是....

.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>

有什么建议吗?

【问题讨论】:

    标签: javascript angularjs regex angularjs-forms


    【解决方案1】:

    使用这个 regex 来验证国际标准电话号码:

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

    【讨论】:

    • SyntaxError: 无效的正则表达式:/^^+d{10}$/: 没有可重复的新 RegExp () --- $scope.rxMobile= "^\+\d {10}\b";
    • @MarcoJr 检查编辑
    【解决方案2】:

    使用正则表达式验证电话号码

    +[\d]{10}$

    【讨论】:

      【解决方案3】:

      好的,我发现了问题所在……

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

      如果我不加引号就可以了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-04
        • 1970-01-01
        • 2013-04-09
        • 1970-01-01
        • 2021-11-28
        • 2021-08-15
        相关资源
        最近更新 更多