Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Need a script that will operate on specific pages (Google Contacts, another website) to format numbers entered into a certain field (phone number) in standard format.

For example, if I enter 1234567890 into the phone number field it will format the number as (123)456-7890.

Have been googling for days and have come up empty handed.

share|improve this question
Ok, what's your actual question? What have you tried already? Where are you getting stuck? – Ƭᴇcʜιᴇ007 Oct 30 '12 at 18:56
What can I use to do this? I haven't found any pre-written scripts that accomplish this. I tried to write my own Greasemonkey script to handle it but could not make it work, could not figure out how to add the onkeyup and onkeydown calls to the input field on the page. If I can figure out how to do that, I think I can use a script like this one in Greasemonkey: – Blue Oct 31 '12 at 14:17
This is the code I am using to add the Javascript calls to the input field but it doesn't seem to work: document.getElementById("person_contact_data_phone_numbers__number").onkeydown ="javascript:backspacerDOWN(this,event);" document.getElementById("person_contact_data_phone_numbers__number").onkeyup ="javascript:backspacerUP(this,event);" – Blue Oct 31 '12 at 14:22
By the way, those function names are for a different formatting script that I had found that I can't find the link to anymore. – Blue Oct 31 '12 at 14:24
Solved my problem. – Blue Oct 31 '12 at 15:34

Solved the problem with the following code (borrowed and slightly modified from the link in my previous comment) as a Greasemonkey script:

function formatPhoneNumber(input){

if (input == null) {return null}
// Strip all non-numeric characters and check for '\'...
var plain = "";
for(var i = 0;i < input.length; i++){
   var c = input.substring(i, i+1);
   if(!isNaN(c) && c != " " || !isNaN(c) && c != "\\" )
      plain += c;
// Make sure we have 10 numeric digits
var tenNumeric = /^[0-9]{10}$/;
   return "("
   + plain.substring(0, 3) 
   + ") "
   + plain.substring(3, 6) 
   + "-"
   + plain.substring(6, 10);
   //Leave it alone
   return input;

var affectedInput = document.getElementById('narf');

affectedInput.onkeyup = function(event) { affectedInput.value = formatPhoneNumber(affectedInput.value);}
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.