how to format phone number (US) in text field using javascript

in this web programming tutorial you will learn that how to format a phone number in US format using javacript in form text field.

copy the below code in section of your file.

function FormatTelephone(txtMob)
                            {
                           
                             var x=txtMob.value;
                              if(txtMob.value)
                              {
                              
                               if(txtMob.value.length<11 && txtMob.value.length > 0 )
                                 {
                                   
                                      alert("Please enter valid length")
                                      txtMob.focus()
                                 }
                              else if(txtHom.value.length != 14) 
                                 {    
                                 var y="("+x.substring(0,4)+")"+" "+x.substring(4,7)+"-"+x.substring(7,12);
                                 txtMob.value=y;
                                 }
                              }
                            }  
                            

Admin

A Software Engineer, Social Media Marketing Expert, writer,

Post a Comment

Previous Post Next Post