Phone number Format in DOM using javascript - Javascript jQuery

Javascript examples for jQuery:Form Input

Description

Phone number Format in DOM using javascript

Demo Code

ResultView the demo in separate window

<html>
 <head> 
  <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js"></script> 
  <script>
$(document).ready(function(){
$("#phone").blur( function (e) {
  var x = e.target.value.replace(/\D/g, '').match(/(\d{3})(\d{3})(\d{4})/);
  e.target.value = '(' + x[1] + ') ' + x[2] + '-' + x[3];
});// w  w w .j a  va2  s.  com
});

      </script> 
 </head> 
 <body> 
  <p>Masked on the blur event (remove focus).</p> 
  <input type="text" id="phone" placeholder="(555) 555-5555">  
 </body>
</html>

Related Tutorials