Validate Phone Number

international phone number

by racha440

HTML

<div ng-controller="Ctrl">
        <label>Phone Number</label>
        <input type="text" name="phone" allowedPattern="[/^[(]{0,1}[0-9]{3}[)\.\- ]{0,1}[0-9]{3}[\-]{0,1}[0-9]{4}$/]"/>
</div>

JavaScript

var app = angular.module('myApp', []);

app.controller('Ctrl', function($scope) {});

app.directive('allowedPattern', function() {
  return {
    restrict: 'A',
    link: function(scope, element, attrs) {
       element.bind('keypress', function(event) {
        var inputString, keyCode, keyCodeChar, regex;
        keyCode = event.which || event.keyCode;
        keyCodeChar = String.fromCharCode(keyCode);
        regex = new RegExp(attrs.allowedPattern, 'g');
        console.log("regex---->"+regex)
        inputString = '' + keyCodeChar;
        console.log("inputString----->"+inputString)
        if (!inputString.match(regex)) {
          event.preventDefault();
        }
      });
    }
  };
});