Home > Blockchain >  How do I remove the last character in some text being copied from one field to another using JavaScr
How do I remove the last character in some text being copied from one field to another using JavaScr

Time:12-03

I have a form that I created to make my work easier and recently figured out how to make certain fields automatically generate a comma and separates after 5 letters or numbers have been typed into it (CPT codes for medical claims that I have to look up) using the same coding you would for putting spaces between numbers. I also have coding here that would force letters to be capitalized since I'm a bit OCD about that stuff for work:

<input name="checkbox24" type="checkbox" id="checkbox24" onClick="FillDetails24(this.form);" /><span style="background-color:yellow">CPT</span>
 <input type = "text" size="8" class = "uc-text-smooth" name="textfield24" id="textfield24" />

<script language = "JavaScript">
const forceKeyPressUppercase = (e) => {
 let el = e.target;
 let charInput = e.keyCode;
 if((charInput >=97) && (charInput <= 122)) { // lowercase
   if(!e.ctrlKey && !e.metaKey && !e.altKey) { // no modifier key
     let newChar = charInput - 32;
     let start = el.selectionStart;
     let end = el.selectionEnd;
     el.value = el.value.substring(0, start)   String.fromCharCode(newChar)   el.value.substring(end);
     el.setSelectionRange(start 1, start 1);
     e.preventDefault();
    }
  }
};

document.querySelectorAll(".uc-text-smooth").forEach(function(current) {
  current.addEventListener("keypress", forceKeyPressUppercase);
});


        document.getElementById('textfield24').addEventListener('input', function (g) {
  g.target.value = g.target.value.replace(/[^\dA-Z]/g, '').replace(/(.{5})/g, '$1, ').trim();
});
</script>

When I use the checkbox, it automatically generates pre-written text using the following JavaScript:

function FillDetails24(f) {
  const elem = f.Reason;
  const x = f.Action;
  const y = f.Resolution;
     f.Reason.value  = ("Verify CPT "   f.textfield24.value   ". "   '\n');
     f.Action.value  = ("Adv on how to locate and use CPT lookup tool on plan website. Information provided in resolution. "   '\n');
 f.Resolution.value  = ("Adv on how to locate and use CPT lookup tool on plan website. Caller is looking to verify CPT "   f.textfield24.value   ". "   '\n'   '\n' );

}

However, because of the way that I put it together, the end result would be, "Adv on how to locate and use CPT lookup tool on plan website. Caller is looking to verify CPT V2020, 99213,. " The extra comma at the end is driving me nuts.

Since it was my first time using this

        document.getElementById('textfield24').addEventListener('input', function (g) {
  g.target.value = g.target.value.replace(/[^\dA-Z]/g, '').replace(/(.{5})/g, '$1, ').trim();
});

with this

function FillDetails24(f) {
  const elem = f.Reason;
  const x = f.Action;
  const y = f.Resolution;
     f.Reason.value  = ("Verify CPT "   f.textfield24.value   ". "   '\n');
     f.Action.value  = ("Adv on how to locate and use CPT lookup tool on plan website. Information provided in resolution. "   '\n');
 f.Resolution.value  = ("Adv on how to locate and use CPT lookup tool on plan website. Caller is looking to verify CPT "   f.textfield24.value   ". "   '\n'   '\n' );

}

I'm not certain how I can code it to eliminate the last comma generated at the end when it pulls the value of textfield24.

This is a very long, complex html form I've coded by hand for fun and for personal use at work for 4 years with only a couple years of HTML training and a little bit of JavaScript I learned in high school forever ago and I've been busting my butt to make this work perfectly so that I only have to tweak the pre-written stuff when things change at work.

I'm at a loss on how to continue. Any suggestions?

CodePudding user response:

you can use substring to remove the last comma, but you have to make sure that the last comma always be there. otherwise, you're gonna remove something else.

const example = "Adv on how to locate and use CPT lookup tool on plan website. Caller is looking to verify CPT V2020, 99213,. ";

const result = example.substring(0, example.lastIndexOf(","))   ".";

console.log(result);

CodePudding user response:

You can replace with regex /,$/.

f = { textfield24: { value: 'V2020, 99213,'} }
console.log(f.textfield24.value);
console.log(f.textfield24.value.replace(/,$/, ''));

function FillDetails24(f) {
  const elem = f.Reason;
  const x = f.Action;
  const y = f.Resolution;
     f.Reason.value  = ("Verify CPT "   f.textfield24.value   ". "   '\n');
     f.Action.value  = ("Adv on how to locate and use CPT lookup tool on plan website. Information provided in resolution. "   '\n');
 f.Resolution.value  = ("Adv on how to locate and use CPT lookup tool on plan website. Caller is looking to verify CPT "   f.textfield24.value.replace(/,$/, '')   ". "   '\n'   '\n' );

}
  • Related