Reputation: 445
I am trying to apply a mask in a phone input. My jQuery version is 1.11 and I am using Masked Input Js with the code below.
Despite that, the mask keep adding an question mark before the last number. What could be causing this?
$.mask.definitions['~'] = "[+-]";
$("#id_phone").focusout(function () {
var phone, element;
element = $(this);
element.unmask();
phone = element.val().replace(/\D/g, '');
if (phone.length > 10) {
element.mask("(99) 99999-999?9");
} else {
element.mask("(99) 9999-9999?9");
}
});
This is the result
Upvotes: 0
Views: 680
Reputation: 633
Can you show yours JS includes And html? I made a phone mask using igorescobar/jQuery-Mask-Plugin, maybe it will help you:
$(function(){
$("#id_phone").on("change", function(){
var phoneLength = 0;
phoneLength = $(this).val().replace(/[^0-9]/g,"").length;
console.log(phoneLength);
if(phoneLength > 10){
$(this).mask('(00) 00000-0000');
}else{
$(this).mask('(00) 0000-00000');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.11/jquery.mask.min.js"></script>
<div class="control-group">
<input type="text" name="phone" id="id_phone" value="" />
</div>
Upvotes: 1