My CodePen: http://codepen.io/leongaban/pen/cyaAL
I have an input field for a phone number which allows up to 20 characters (for international numbers).
I'm also using the Masked input jQuery plugin by Josh Bush to format the phone number in the input to make it 'pretty'.
My problem is that in the requirements when the phone is 10 digits or less, it should use the Masked input formatting.
However when the phone number is longer then 10 digits, the formatting should be removed.
Here is my current: CodePen, Cell Phone is the input field where I'm trying to accomplish this. Work Phone is an example of the default functionality of the Mask input plugin.
How would you go about this problem?
jQuery for Cell Phone input field:
case '2':
console.log('created phone input');
$('.new_option').append(myphone);
$('.added_mobilephone').mask('(999) 999-9999? 9');
$('.added_mobilephone').keypress(function(event){
if (this.value.trim().length > 10) {
console.log('this.value = '+this.value.trim());
console.log('greater then 10');
$('.added_mobilephone').mask('99999999999999999999');
}
/*if (this.value.length < 9) {
console.log(this.value);
console.log('less then 10');
$('.added_mobilephone').mask('(999) 999-9999? 9999999999');
} else if (this.value.length > 9) {
console.log(this.value);
console.log('greater then 10');
$('.added_mobilephone').mask('99999999999999999999');
}*/
});
break;
Input type text to input type telephone number. max-length, placeholder using jquery.
Similar solution above but in this script the format is applying meanwhile the user is typing. Adding mask in the phone number - Brazilian Phone Format (00) ?0000-0000. It can be helpful for others as well.