Important alert: (current site time 7/15/2013 12:55:13 PM EDT)
 

VB icon

Autofill phone number

Email
Submitted on: 5/26/2005 4:35:29 PM
By: James Fitch 
Level: Beginner
User Rating: By 1 Users
Compatibility: JavaScript
Views: 35996
(About the author)
 
     I got sick of all the code snippets for filling in the "(",")", and "-" in phone numbers because in none of them could you use the backspace key if you made a mistake (They'd fill the backspaced entry out and you'd be back where ouy started)
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
' for :Autofill phone number
'**************************************
<FORM name=frmPhone>
<font size="4" color="#0000FF"><b>Enter Telephone Number</b></font><br>
Watch phone number change to (###)###-#### format<br>
<input type=hidden name=phone id=phone>
<INPUT type="text" name="txtphone" maxlength="15" id="phonetxt" value="()-" onkeyup="ValidatePhone()"><br>
<INPUT type=reset value=Reset name=reset>
</FORM>
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Autofill phone number
' Description:I got sick of all the code snippets for filling in the "(",")", and "-" in phone numbers because in none of them could you use the backspace key if you made a mistake (They'd fill the backspaced entry out and you'd be back where ouy started)
' By: James Fitch
'
' Assumes:Doesn't seem to work on older versions of netscape. Don't know why, and haven't spent time trying to fix it.
Also, this keeps the raw phone number in a hidden field for form submission so that you can pick it up either way on the other end.
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=4833&lngWId=14'for details.'**************************************

var p1;	//conversion phone number
var p2; //conversion phone text
var mask="()-"; //this is what phone numbers look like
var oldvalue=""; //the last thing in phonetxt
function ValidatePhone()
{
	p1=frmPhone.phonetxt.value
	if (p1.length > oldvalue.length)
	{
		p1=p1.replace("(","");
		p1=p1.replace(")","");
		p1=p1.replace("-","");
		while (p1.search(" ") != -1)
		{
			p1=p1.replace(" ","");
		}
		
		frmPhone.phone.value = p1;
		if (p1.length < 4)
		{
			p2=mask.substring(p1.length + 1,13);
			frmPhone.phonetxt.innerText="(" + p1 + p2;
		} else {
			if (p1.length < 7)
			{
				p2=mask.substring(p1.length + 2,13);
				frmPhone.phonetxt.innerText="(" + p1.substring(0,3) + ")" + p1.substring(3,6) + p2;
			} else {
				p2=mask.substring(p1.length + 3,13)
				frmPhone.phonetxt.innerText="(" + p1.substring(0,3) + ")" + p1.substring(3,6) + "-" + p1.substring(6,10) + p2;
			}
		}
	}
	oldvalue=frmPhone.phonetxt.value;
}
		


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
1/4/2007 9:16:49 AMaugustine

ya ,it's very useful for me
(If this comment was disrespectful, please report it.)

 
1/6/2009 4:52:45 PMkodel

thanx..
it's very help me for my homework..
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.