admin管理员组文章数量:1389758
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 (.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:".1.5/js/utils.js",
autoPlaceholder: true,
initialCountry:"us",
geoIpLookup:"auto",
});
});
<!DOCTYPE html>
<html>
<head>
<title>Phone</title>
<link rel="stylesheet" href=".1.5/css/intlTelInput.css">
<link rel="stylesheet" href="./intlmask.css">
<script src=".1.1/jquery.min.js"></script>
<script src=".1.5/js/intlTelInput.min.js"></script>
<script src=".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>
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./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./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./ajax/libs/intl-tel-input/12.1.5/css/intlTelInput.css">
<link rel="stylesheet" href="./intlmask.css">
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/intl-tel-input/12.1.5/js/intlTelInput.min.js"></script>
<script src="https://cdnjs.cloudflare./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
Share Improve this question edited Dec 6, 2017 at 20:36 jsPlayer asked Dec 6, 2017 at 20:28 jsPlayerjsPlayer 1,2457 gold badges34 silver badges56 bronze badges 3- 2 Your snippet appears to work perfectly fine for me. What's not working with it? – Obsidian Age Commented Dec 6, 2017 at 20:33
- @ObsidianAge hey i added a pic on the question, i want to format number as user blur out of input – jsPlayer Commented Dec 6, 2017 at 20:37
-
@jsPlayer which place you have to download this two file
./intlmask.js
and ./intlmask.css – Rahul Patil Commented Oct 17, 2020 at 7:18
3 Answers
Reset to default 1so 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);
}
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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery./jquery-1.12.4.js"></script>
<script src="https://code.jquery./ui/1.12.1/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare./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>
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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery./jquery-1.12.4.js"></script>
<script src="https://code.jquery./ui/1.12.1/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare./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>
本文标签: javascriptInternational Telephone Input number formatting way aroundStack Overflow
版权声明:本文标题:javascript - International Telephone Input number formatting way around? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744690240a2619950.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论