Validate phone number using angular js

前端 未结 9 487
情歌与酒
情歌与酒 2020-12-03 00:13

Want to set phone-number to 10 digits, How can I do this using Angular js.

This is what I have tried:



        
相关标签:
9条回答
  • 2020-12-03 00:27

    Try this:

    <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>
    
    0 讨论(0)
  • 2020-12-03 00:32

    You can also use ng-pattern and I feel that will be a best practice. Similarly try to use ng-message. Please look the ng-pattern attribute on the following html. The code snippet is partial but hope you understand it.

    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>
    
    0 讨论(0)
  • 2020-12-03 00:34

    An even cleaner and more professional look I have found is to use AngularUI Mask. Very simple to implement and the mask can be customized for other inputs as well. Then a simple required validation is all you need.

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

    0 讨论(0)
  • 2020-12-03 00:39

    You can also use ng-pattern ,[7-9] = > mobile number must start with 7 or 8 or 9 ,[0-9] = mobile number accepts digits ,{9} mobile number should be 10 digits.

    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>

    0 讨论(0)
  • 2020-12-03 00:40
    <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>
    

    Here "userForm" is my form name.

    0 讨论(0)
  • 2020-12-03 00:44

    Use ng-pattern, in this example you can validate a simple patern with 10 numbers, when the patern is not matched ,the message is show and the button is disabled.

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

    Also you can use another complex patern like

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

    , for more complex phone numbers

    0 讨论(0)
提交回复
热议问题