Answers for "autoformat mobile number (xxx) xxx-xxxx"

0

autoformat mobile number (xxx) xxx-xxxx

// For this type format xxx-xx-xxxx
$(document).on('keyup','your input element',function(){
   let text=$(this).val()                        
   text=text.replace(/\D/g,'')  
    
   if(text.length > 3) text=text.replace(/.{3}/,'$&-')  
   if(text.length > 6) text=text.replace(/.{6}/,'$&-')  
   $(this).val(text.substring(0,11));   
});
//For this type format (999) 999-9999
$(document).on('keyup','Your input element',function(e){

   let text=$(this).val()                        
   text=text.replace(/\D/g,'')  
    
   text=text.replace(/.{0}/,'$& ( ' )
   text=text.replace(/.{6}/,'$& ) '); //Add hyphen at pos.4
   text=text.replace(/.{12}/,'$&-')   //Add hyphen at pos.8
   $(this).val(text.substring(0,17)); //Set the new text
});
Posted by: Guest on November-23-2020

Code answers related to "autoformat mobile number (xxx) xxx-xxxx"

Code answers related to "Javascript"

Browse Popular Code Answers by Language