Skip to main content
Known Participant
February 8, 2018
Answered

Need help converting numbers to letters

  • February 8, 2018
  • 6 replies
  • 2313 views

I'm creating a PDF form to be used in a blood bank.  When I scan a number, from a blood bag, how can I convert it to text?  For instance, I want the numbers "5100" to convert to "O Pos".  Is there a Javascript that can do that?

This topic has been closed for replies.
Correct answer try67

Let's say the field with the numbers is called Text1, and the field where the text should appear is called Text2.

Use something like this as the custom calculation script for Text2:

var t1 = this.getField("Text1").valueAsString;

if (t1=="5100") event.value = "O Pos";

else if (t1=="5200") event.value = "O Neg";

else if (t1=="5300") event.value = "A Pos";

else if (t1=="5400") event.value = "A Neg"; // etc.

else event.value = "";

Of course, I just made up the values above... Replace them with the real ones in your actual code.

6 replies

Known Participant
February 9, 2018

Ok, thanks for the clarification.  I appreciate all your help!

Known Participant
February 9, 2018

I asked those questions too soon.  I figured out how to hide the field, and to delete the numbers at the beginning of each line.  Thank you again, for that script!

Here is another question, though.  Can you write me a script that cleaves off the beginning character of whatever is scanned?  For instance, when I scan a blood identification number that is supposed to be W123456789123, it shows up as =W123456789123.  I want to automatically remove the "=".  Thanks!

try67
Community Expert
Community Expert
February 9, 2018

The problem is I'm not sure what kind of events the scanning processes triggers... If it's the same as entering the value manually into the field then you can use this code as the custom validation script:

event.value = event.value.replace(/^=/, "");

Known Participant
February 9, 2018

Yes, the scanning process triggers the same event as entering the value manually.  I will try this script.

Back to the original script.  I can get it to convert the numerical value to the alphabetic value, but only when I'm editing the Text field properties.  I falsely assumed that it would work as I was scanning it.  I added the following script as a Custom Keystroke Script to move the cursor from one field to the next:

// Autotab to the "Comp  Code Row1" field

tab_next("Comp  Code Row1");

And it works for all the fields except for the ABO1 field.  Why would it only do the conversion when I'm editing it, and not after I've closed the form editing mode?

Known Participant
February 9, 2018

I'm obviously not a software engineer, I'm a clinical laboratory scientist, so I'm doing my best to understand the details, but I am an amateur at best.  I inserted the real ABO values in the code, and deleted the "// etc."  Was is correct to deleted the "//"?  And, when I paste it into the form, do I need to delete the numbers at the beginning of each line?

Known Participant
February 9, 2018

OK.  How do I hide the first field?

Known Participant
February 9, 2018

Thank you for sending the script.  Must I create two different fields, or is there a way to scan into a single field, and have the interpretation show up in that same field?  Or, can I overlay the two fields somehow?  This is a screen shot of the current form.  I don't like the drop down menu under the Blood Type column because it can't be scanned like the two fields prior to it in the same row (I already inserted a script to auto-tab from one field to the next).  I want to scan the blood type ("5100") in the "ABO1" field and have the interpretation of "O Pos" show up in the same spot (and then auto-tab to the next row).  Is that possible?

try67
Community Expert
Community Expert
February 9, 2018

I would use two fields. The first one can even be hidden.

try67
Community Expert
Community Expert
February 8, 2018

Do these numbers appear in a form field? If so, then it can be done using a custom calculation script, yes.

Known Participant
February 8, 2018

Yes, they are scanned into a form field.  Can you post the custom calculation script?  Thanks!

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
February 8, 2018

Let's say the field with the numbers is called Text1, and the field where the text should appear is called Text2.

Use something like this as the custom calculation script for Text2:

var t1 = this.getField("Text1").valueAsString;

if (t1=="5100") event.value = "O Pos";

else if (t1=="5200") event.value = "O Neg";

else if (t1=="5300") event.value = "A Pos";

else if (t1=="5400") event.value = "A Neg"; // etc.

else event.value = "";

Of course, I just made up the values above... Replace them with the real ones in your actual code.