Copy link to clipboard
Copied
I am trying to add custom formatting to a form field containing a superscript. Specifically, this form will have units of measurement tied to all its fields, and some are units squared. I have the following taken from another post for formatting in feet with 2 decimal places:
if((event.value == "") || (event.value == 0))
event.value = "";
else
event.value = util.printf("%,0.2f feet",event.value);
to be placed in the custom format script section on the format tab for a field. I do not have a full grasp of the printf syntax, but I think I can adapt it to my needs once I know how the supercript should work.
So I need help having a field display ft2 with the 2 shown as a superscript.
Thanks in advanced.
JM
Superscripts fall in to the category of Rich Text, i.e. marked up text. For that the field needs to be set to accecpt rich text and the content for the field is built with spans. Here's the reference entry for spans, it includes an example.
This is a bit more than just the normal text formatting.
Thom's solution will work, but there's a much easier one, since there's actually a special Unicode character for a superscript 2, and the code for it can be added directly to your original script, like this:
if((event.value == "") || (event.value == 0))
event.value = "";
else
event.value = util.printf("%,0.2f feet\u00B2",event.value);
Copy link to clipboard
Copied
Superscripts fall in to the category of Rich Text, i.e. marked up text. For that the field needs to be set to accecpt rich text and the content for the field is built with spans. Here's the reference entry for spans, it includes an example.
This is a bit more than just the normal text formatting.
Copy link to clipboard
Copied
So if the field is formatted ahead of time to accept rich text, can I make use of spans in the custom format script? Can someone help me move forward in that direction?
I've just tried this, but it doesn't even accept it as I have it...
if((event.value == "") || (event.value == 0)){
event.value = "";
}else{
var spans new Array();
spans[0] = new Object();
spans[0].text = event.value;
spans[1] = new Object();
spans[1].text = " ft";
spans[2] = new Object();
spans[2].text = "2";
spans[2].superscript = true;
event.richValue = spans;
}
This gives me this error:
SyntaxError: missing ; before statement
4:
Copy link to clipboard
Copied
This line is incorrect:
var spans new Array();
needs to be
var spans = new Array();
Looks like you are on the right track.
Copy link to clipboard
Copied
Ah, the dreaded typo! Thank you, Thom. It is working for me now!
Copy link to clipboard
Copied
You can add the number formatting to the first span.
spans[0].text = util.printf("%,0.2f feet",event.value);
Copy link to clipboard
Copied
Thom's solution will work, but there's a much easier one, since there's actually a special Unicode character for a superscript 2, and the code for it can be added directly to your original script, like this:
if((event.value == "") || (event.value == 0))
event.value = "";
else
event.value = util.printf("%,0.2f feet\u00B2",event.value);
Copy link to clipboard
Copied
Ah, this is even cleaner. Thank you everyone for the help. Both solutions work, but I am marking this one as it is more in line with what I was seeking.