15

想要将电话号码设置为10位数字,我该如何使用 Angular js 来做到这一点。

这是我尝试过的:

<form class="form-horizontal" role="form" method="post" name="registration" novalidate>
  <div class="form-group" ng-class="{'has-error': registration.phone.$error.number}">
    <label for="inputPhone" class="col-sm-3 control-label">Phone :</label>
    <div class="col-sm-9">
      <input type="number" 
             class="form-control" 
             ng-minlength="10" 
             ng-maxlength="10"  
             id="inputPhone" 
             name="phone" 
             placeholder="Phone" 
             ng-model="user.phone" 
             ng-required="true">
      <span class="help-block" 
            ng-show="registration.phone.$error.required && 
                     registration.phone.$error.number">
                     Valid phone number is required
      </span>
      <span class="help-block" 
            ng-show="((registration.password.$error.minlength || 
                      registration.password.$error.maxlength) && 
                      registration.phone.$dirty) ">
                      phone number should be 10 digits
       </span>
    </div>
  </div>
</form>

但我没有收到验证错误。

4

9 回答 9

14

尝试这个:

<form class="form-horizontal" role="form" method="post" name="registration" novalidate>
    <div class="form-group" ng-class="{'has-error': registration.phone.$error.number}">
        <label for="inputPhone" class="col-sm-3 control-label">Phone :</label>
        <div class="col-sm-9">
            <input type="number" 
                   class="form-control" 
                   ng-minlength="10" 
                   ng-maxlength="10"  
                   id="inputPhone" 
                   name="phone" 
                   placeholder="Phone" 
                   ng-model="user.phone"
                   ng-required="true">
            <span class="help-block" 
                  ng-show="registration.phone.$error.required || 
                           registration.phone.$error.number">
                           Valid phone number is required
            </span>
            <span class="help-block" 
                  ng-show="((registration.phone.$error.minlength ||
                           registration.phone.$error.maxlength) && 
                           registration.phone.$dirty) ">
                           phone number should be 10 digits
            </span>
于 2015-01-16T14:56:40.283 回答
8

检查这个答案

基本上,您可以创建一个正则表达式来满足您的需求,然后将该模式分配给您的输入字段。

或者更直接的方法:

<input type="number" require ng-pattern="<your regex here>">

更多信息@ angular docs herehere(内置验证器)

于 2015-01-16T14:36:56.430 回答
4

您也可以使用 ng-pattern ,[7-9] => 手机号码必须以 7 或 8 或 9 开头 ,[0-9] = 手机号码接受数字 ,{9} 手机号码应为 10 位数。

function form($scope){
    $scope.onSubmit = function(){
        alert("form submitted");
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
<div ng-app ng-controller="form">
<form name="myForm" ng-submit="onSubmit()">
    <input type="number" ng-model="mobile_number" name="mobile_number" ng-pattern="/^[7-9][0-9]{9}$/" required>
    <span ng-show="myForm.mobile_number.$error.pattern">Please enter valid number!</span>
    <input type="submit" value="submit"/>
</form>
</div>

于 2016-12-26T17:21:14.697 回答
3

你也可以使用ng-pattern,我觉得这将是一个最佳实践。同样尝试使用ng-message. 请查看以下 html 中的 ng-pattern 属性。代码片段是部分的,但希望你能理解。

angular.module('myApp', ['ngMessages']);
angular.module("myApp.controllers",[]).controller("registerCtrl", function($scope, Client) {
  $scope.ph_numbr = /^(\+?(\d{1}|\d{2}|\d{3})[- ]?)?\d{3}[- ]?\d{3}[- ]?\d{4}$/;
});
<form class="form-horizontal" role="form" method="post" name="registration" novalidate>
  <div class="form-group" ng-class="{ 'has-error' : (registration.phone.$invalid || registration.phone.$pristine)}">
    <label for="inputPhone" class="col-sm-3 control-label">Phone :</label>
    <div class="col-sm-9">
      <input type="number" class="form-control" ng-pattern="ph_numbr"  id="inputPhone" name="phone" placeholder="Phone" ng-model="user.phone" ng-required="true">
      <div class="help-block" ng-messages="registration.phone.$error">
        <p ng-message="required">Phone number is required.</p>
        <p ng-message="pattern">Phone number is invalid.</p>
      </div>
    </div>
  </div>
</form>
于 2016-01-24T02:55:35.250 回答
2

使用 ng-intl-tel-input 验证所有国家/地区的手机号码。您也可以设置默认国家/地区。在 npm 上:https ://www.npmjs.com/package/ng-intl-tel-input

更多详情:https ://techpituwa.wordpress.com/2017/03/29/angular-js-phone-number-validation-with-ng-intl-tel-input/

于 2017-03-29T19:28:16.410 回答
1
<form name = "numberForm">
    <div>
    <input type="number"
           placeholder = "Enter your phonenumber"
           class = "formcontroll"
           name = "numbers"
           ng-minlength = "10"
           ng-maxlength = "10"
           ng-model="phno" required/>
           <p ng-show = "numberForm.numbers.$error.required ||
                       numberForm.numbers.$error.number">
                       Valid phone number is required</p>
            <p ng-show = "((numberForm.numbers.$error.minlength ||
                        numberForm.numbers.$error.maxlength)
                        && numberForm.numbers.$dirty)">
                        Phone number should be 10 digits</p><br><br>
       </div>
   </form>
于 2016-04-21T13:30:50.947 回答
1

我发现一个更干净、更专业的外观是使用 AngularUI Mask。实现起来非常简单,也可以为其他输入定制掩码。然后,您只需要一个简单的必需验证。

https://angular-ui.github.io/

于 2015-11-24T18:39:07.387 回答
0
<div ng-class="{'has-error': userForm.mobileno.$error.pattern ,'has-success': userForm.mobileno.$valid}">

              <input type="text" name="mobileno" ng-model="mobileno" ng-pattern="/^[7-9][0-9]{9}$/"  required>

这里“userForm”是我的表单名称。

于 2018-07-19T15:16:01.220 回答
0

使用 ng-pattern,在本例中,您可以验证一个包含 10 个数字的简单模式,当模式不匹配时,显示消息并禁用按钮。

 <form  name="phoneNumber">

        <label for="numCell" class="text-strong">Phone number</label>

        <input id="numCell" type="text" name="inputCelular"  ng-model="phoneNumber" 
            class="form-control" required  ng-pattern="/^[0-9]{10,10}$/"></input>
        <div class="alert-warning" ng-show="phoneNumber.inputCelular.$error.pattern">
            <p> write a phone number</p>
        </div>

    <button id="button"  class="btn btn-success" click-once ng-disabled="!phoneNumber.$valid" ng-click="callDigitaliza()">Buscar</button>

你也可以使用另一个复杂的模式,比如

^+?\d{1,3}?[- .]?(?(?:\d{2,3}))?[- .]?\d\d\d[- .]?\d\ d\d\d$

, 对于更复杂的电话号码

于 2016-11-04T18:16:14.573 回答