Jump to content

Multiple JavaScripts for formatting


Recommended Posts

I am trying to format multiple fields on a page. The first is the phone number, which I found the JavaScript for on the JavaScript forum. My issue is that I need to format phone number, social, and a tax-ID on one page and I am unsure how to accomplish this task. For phone number I am using the script below, plus I found another for formatting a social on StackExchange, but my JavaScript reading/writing skills are sub-par and I did not combine these correctly. Then I want to add in the option to format the tax ID as well, which is ##-####### (2 digits, dash, 7 digits). 

<SCRIPT LANGUAGE="JavaScript">
function f_a(v_id)
{
return document.getElementById(v_id);
}
f_a('EditRecordPhone_Number').maxLength = 14;
f_a('EditRecordPhone_Number').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));
};
}
function(){
document.getElementById("SSN").onkeyup = function() {
var val = this.value.replace(/\D/g, '');
var newVal = '';

if(val.length > 4) {
this.value = val;
}

if((val.length > 3) && (val.length < 6)) {
newVal += val.substr(0, 3) + '-';
val = val.substr(3);
}

if (val.length > 5) {
newVal += val.substr(0, 3) + '-';
newVal += val.substr(3, 2) + '-';
val = val.substr(5);
}

newVal += val;
this.value = newVal;
};
</SCRIPT>

Link to comment
Share on other sites

  • 6 years later...

Hi! If you would like to automatically format SSN/Social Security Number in the Text Field, you can try these solutions:

1. From this link: https://stackoverflow.com/questions/7685175/autoformat-ssn-while-entering-the-number#:~:text=at 0%3A25-,Karl Keefer,-6361 - JavaScript

Applying in the DataPage's Footer with this code:

<script type="text/javascript">
document.addEventListener('DataPageReady', function (event) {

function formatSSN(ssn) {

  // remove all non-dash and non-numerals
  var val = ssn.replace(/[^\d-]/g, '');

  // add the first dash if number from the second group appear
  val = val.replace(/^(\d{3})-?(\d{1,2})/, '$1-$2');

  // add the second dash if numbers from the third group appear
  val = val.replace(/^(\d{3})-?(\d{2})-?(\d{1,4})/, '$1-$2-$3');

  // remove misplaced dashes
  val = val.split('').filter((val, idx) => {
    return val !== '-' || idx === 3 || idx === 6;
  }).join('');

  // enforce max length
  return val.substring(0, 11);

}

// bind our function
document.getElementById("InsertRecordSSN").onkeyup = function(e) {
  this.value = formatSSN(this.value);

}

});

</script>

2. https://stackoverflow.com/questions/7685175/autoformat-ssn-while-entering-the-number#:~:text=at 6%3A20-,Jason Hansen,-33 - jQuery
 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<script type="text/javascript">
document.addEventListener('DataPageReady', function (event) {

 $('#InsertRecordSSN').keyup(function() {
          var val = this.value.replace(/\D/g, '');
          var newVal = '';
          if(val.length > 4) {
             this.value = val;
          }
          if((val.length > 3) && (val.length < 6)) {
             newVal += val.substr(0, 3) + '-';
             val = val.substr(3);
          }
          if (val.length > 5) {
             newVal += val.substr(0, 3) + '-';
             newVal += val.substr(3, 2) + '-';
             val = val.substr(5);
           }
           newVal += val;
           this.value = newVal.substring(0, 11);
        });

});

</script>

3. By using Cleave JS - https://nosir.github.io/cleave.js/

<script src="https://cdnjs.cloudflare.com/ajax/libs/cleave.js/1.6.0/cleave.min.js"></script>

<script>

var cleave = new Cleave('#InsertRecordSSN', {
    delimiter: '-',
    blocks: [3,2,4],
    numericOnly: true
});

</script>

Take note that you need to change the InsertRecordSSN based on your right field name. 

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