Jump to content

Change phone marsking script to update three fields


Recommended Posts

I have a script that formats the phone number input to a particular mask, which works great.

There are three fields in this Datapage that need the same masking script and currently I have copied the code three times and simply changed the field that it refers to (code below). I am wondering if there is a way to add an array to this so that if you update either the Finance_Phone, Primary_Phone or Additional_phone the script will run. 

That way the full script is on the page only once and not copied three times.

 

function f_a(v_id)
{
return document.getElementById(v_id);
}
f_a('EditRecordFinance_Phone').maxLength = 14;
f_a('EditRecordFinance_Phone').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));
};
}

 

Link to comment
Share on other sites

Try this

 

var o_els = new Array("EditRecordFinance_Phone", "EditRecordFinance_Phone2", "EditRecordFinance_Phone3") ;

function f_cap() {
	for (var i = 0; i < o_els.length; i++) {
		
function f_a(v_id)
{
return document.getElementById(v_id);
}
f_a('o_els[i]').maxLength = 14;
f_a('o_els[i]').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));
};
}
}

replace EditRecordFinance_Phone2 and EditRecordFinance_Phone3 with actual field IDs

Link to comment
Share on other sites

  • 1 year later...

Here is the code:

 

<SCRIPT LANGUAGE="JavaScript">

var o_els = new Array("InsertRecordHome_Phone", "InsertRecordCell_Phone") ;
for (var i = 0; i < o_els.length; i++) {

document.getElementById(o_els[i]).maxLength = 14;
document.getElementById(o_els[i]).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

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...