wgalliance Posted September 19, 2016 Report Share Posted September 19, 2016 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)); }; } Quote Link to comment Share on other sites More sharing options...
MayMusic Posted September 20, 2016 Report Share Posted September 20, 2016 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 Quote Link to comment Share on other sites More sharing options...
MayMusic Posted January 29, 2018 Report Share Posted January 29, 2018 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.