jsPlayer
jsPlayer

Reputation: 1245

International Telephone Input number formatting way around?

As I understand they have removed the autoformat option on the latest International Telephone Input library.This option formatted the number accordingly to the country the user choose. But according to the forms, i can still use util.js and format-number function to achieve this options (https://github.com/jackocnr/intl-tel-input/wiki/utils.js) I tried multiple times but not being able to apply this option. For example for US Number, how would I format the number to this ex:(201)-255-6655. This is my code below. As I see this seems to be the best js library for international numbers.

$( document ).ready(function() {

    $('#contactForm')
    .find('[name="phoneNumber"]')
        .intlTelInput({
            utilsScript:"https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/12.1.5/js/utils.js",
          
            autoPlaceholder: true,
            initialCountry:"us",
            geoIpLookup:"auto",

        });


    });
<!DOCTYPE html>
<html>
<head>
<title>Phone</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/12.1.5/css/intlTelInput.css">
<link rel="stylesheet" href="./intlmask.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/12.1.5/js/intlTelInput.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/12.1.5/js/utils.js"></script>

<script src="./intlmask.js"></script>
</head>
<body>

  <form id="contactForm" class="form-horizontal">
    <div class="form-group">
        <label class="col-xs-3 control-label">Phone number</label>
        <div class="col-xs-5">
            <input type="tel" class="form-control" name="phoneNumber" />
        </div>
    </div>
</form>

</body>
</html>

like this enter image description here

Upvotes: 2

Views: 12104

Answers (3)

Dinesh Guragain
Dinesh Guragain

Reputation: 1

Use these regexes:

function phoneFormatter() {
  $('.phone').on('input', function() {
    var number = $(this).val().replace(/[^\d]/g, '')
    if (number.length == 7) {
      number = number.replace(/(\d{3})(\d{4})/, "$1-$2");
      //US & Canada Formatting
    } else if (number.length == 10) {
      number = number.replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3");
    }
      //France Formatting
      else if (number.length == 11) {
      number = number.replace(/(\d{2})(\d{1})(\d{2})(\d{2})(\d{2})(\d{2})/, "+$1 $2 $3 $4 $5 $6");
    }
      //German Formattiing 
      else if (number.length == 13) {
      number = number.replace(/(\d{2})(\d{3})(\d{8})/, "+$1 $2 $3");
    }
    $(this).val(number)
  });
};

$(phoneFormatter);
 <script type="text/javascript” src=”/_layouts/MicrosoftAjax.js" ></script>
<script type="text/javascript” src=”/_layouts/SP.debug.js" ></script>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/validator/8.2.0/validator.js"></script>
<html>
<body>
 <p><label>Phone Number<br>               
 <input name="phone"    class="phone" id="phone"     type="text"     required></label></p>

</body>
</html>

Upvotes: 0

na12063
na12063

Reputation: 33

function phoneFormatter() {
  $('.phone').on('input', function() {
    var number = $(this).val().replace(/[^\d]/g, '')
    if (number.length == 7) {
      number = number.replace(/(\d{3})(\d{4})/, "$1-$2");
      //US & Canada Formatting
    } else if (number.length == 10) {
      number = number.replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3");
    }
      //France Formatting
      else if (number.length == 11) {
      number = number.replace(/(\d{2})(\d{1})(\d{2})(\d{2})(\d{2})(\d{2})/, "+$1 $2 $3 $4 $5 $6");
    }
      //German Formattiing 
      else if (number.length == 13) {
      number = number.replace(/(\d{2})(\d{3})(\d{8})/, "+$1 $2 $3");
    }
    $(this).val(number)
  });
};

$(phoneFormatter);
 <script type="text/javascript” src=”/_layouts/MicrosoftAjax.js" ></script>
<script type="text/javascript” src=”/_layouts/SP.debug.js" ></script>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/validator/8.2.0/validator.js"></script>
<html>
<body>
 <p><label>Phone Number<br>               
 <input name="phone"    class="phone" id="phone"     type="text"     required></label></p>

</body>
</html>

Upvotes: 1

jsPlayer
jsPlayer

Reputation: 1245

so I basically added a function and ran it when user-focused out of the input. utils.js already provide us with formatting (intlTelInputUtils.formatNumber). we just have to pass the number and country code/initial to it. check my code below

    function test(){
        var number =  $('input[name="phoneNumber"]').val();
        var classf = $(".selected-flag > div").attr("class");
        var flag = classf.slice(-2);
  
        console.log("check number: ", number);
        console.log("check classf: ", classf);
        console.log("check flag: ", flag);
        
        var formattedNumber = intlTelInputUtils.formatNumber(number, flag, intlTelInputUtils.numberFormat.INTERNATIONAL);
        $('input[name="phoneNumber"]').val(formattedNumber);

}

Upvotes: 1

Related Questions