larsonchst Posted November 9, 2015 Report Share Posted November 9, 2015 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> Quote Link to comment Share on other sites More sharing options...
iren Posted November 10, 2015 Report Share Posted November 10, 2015 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 Quote Link to comment Share on other sites More sharing options...
larsonchst Posted November 10, 2015 Author Report Share Posted November 10, 2015 Iren, thank you! It works perfectly. Quote Link to comment Share on other sites More sharing options...
iren Posted November 10, 2015 Report Share Posted November 10, 2015 I am glad my suggestion helped 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.