国际电话输入数字格式化方式?

时间:2017-12-06 20:28:50

标签: javascript jquery libphonenumber intl-tel-input

据我了解,他们已删除最新国际电话输入库上的autoformat选项。此选项会根据用户选择的国家/地区格式化该号码。但是根据表单,我仍然可以使用util.js和format-number函数来实现这个选项(https://github.com/jackocnr/intl-tel-input/wiki/utils.js)我试过多次但是无法应用这个选项。例如,对于US Number,我如何将数字格式化为此ex:(201)-255-6655。这是我的代码。我认为这似乎是国际数字最好的js库。

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

像这样 enter image description here

2 个答案:

答案 0 :(得分:1)

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>

答案 1 :(得分:0)

所以我基本上添加了一个函数,并在用户关注输出时运行它。 utils.js已经为我们提供了格式化(intlTelInputUtils.formatNumber)。我们只需要将数字和国家/地区代码/首字母传递给它。检查下面的代码

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

}