Jump to content

Modifying The Telephone Code (Javascript Help)


Recommended Posts

I have a javascript code I got off of this forum that takes an inputted ten digit number and adds formatting (ie 1234567891 gets changed to (123) 456-7891). 

 

I have a field that will always be ######-###### and I'd like to modify the code to add the dash in the middle. What I'm trying to avoid is having someone needing to click on multiple boxes to fill out the entire field. 

 

I'm not great with javascript, so I was hoping someone on here would be able to help me with that.

 

Here is the code:

 

<SCRIPT LANGUAGE="JavaScript">
function f_a(v_id)
{
return document.getElementById(v_id);
}
f_a('InsertRecordPhone').maxLength = 14;
f_a('InsertRecordPhone').onkeyup = function(v_e)
{
v_e = v_e || window.event;
if (v_e.keyCode >= 65 && v_e.keyCode <= 90){
this.value = this.value.substr(0, this.value.length - 1);
return false;
}else if (v_e.keyCode >= 37 && v_e.keyCode <= 40){
return true;
}
var v_value =(this.value.replace(/[^\d]/g, ''));
if (v_value.length==7) {
this.value = (v_value.substring(0,3) + "-" + v_value.substring(3,7));}
else if(v_value.length==10){
this.value = ("(" + v_value.substring(0,3) + ") " + v_value.substring(3,6) + "-" + v_value.substring(6,10));
};
}

 

</SCRIPT>
Link to comment
Share on other sites

Hello larsonchst,

 

You can add this code to the Footer (do not forget clicking the Source button and changing FIELDNAME to the name of your field):

<SCRIPT LANGUAGE="JavaScript">
function f_a(v_id)
{
return document.getElementById(v_id);
}
f_a('InsertRecordFIELDNAME').maxLength = 14;
f_a('InsertRecordFIELDNAME').onkeyup = function(v_e)
{
v_e = v_e || window.event;
if (v_e.keyCode >= 65 && v_e.keyCode <= 90){
this.value = this.value.substr(0, this.value.length - 1);
return false;
}else if (v_e.keyCode >= 37 && v_e.keyCode <= 40){
return true;
}
var v_value =(this.value.replace(/[^\d]/g, ''));
if (v_value.length==12){
this.value = (v_value.substring(0,6) + "-" + v_value.substring(6,12));}
}
</SCRIPT>

Hope it helps.

 

IREN

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...