0

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?

  • Welcome to SO! I recommend all new users visit [ask] for tips on writing posts on the site that best enable the community to assist you. I suspect there is a relatively simple answer to your question, but it is a bit hard to tell because there a lot of code to parse through. Would it be possible to pare this down to a [mcve] that illustrates just the issue and nothing else? This may help clarify what you are trying to accomplish. Good luck, and happy coding! – Alexander Nied Dec 03 '22 at 02:34
  • If the question is simply [how to remove the last character of a string, then [it is probably a duplicate of this post](https://stackoverflow.com/q/7438612/6831341). If you need to do it conditionally, you could wrap it with an `if`. – Alexander Nied Dec 03 '22 at 02:35
  • Not sure if this helps for your particular application but when dealing with lists, I often accumulate the strings into an array (using `array.push("message part")`) and then build the final record as the string returned from `array.join(", ")` – Dave Pritlove Dec 03 '22 at 02:35

2 Answers2

0

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);
Layhout
  • 1,445
  • 1
  • 3
  • 16
0

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' );

}
wei
  • 937
  • 2
  • 14
  • 34
  • Wei, holy guacamole, you figured it out! I am very new to regex and have only just recently started looking into it more. I'm still considered a novice in a loooooot of areas in html. This fixes literally everything I was worried about! Thank you, thank you, thank you!! – Amy Barnett Dec 03 '22 at 06:23