You are viewing limited content. For full access, please sign in.

Question

Question

Write character count to another field in Modern Designer

asked on December 6, 2024

How can I write the character count to a single line field in order to prevent submitting unless they have the correct amount of characters? I am using this script, but it writes to the console and the subtext, not a field. 

 

const charCount = LFForm.findFieldsByFieldId(1)[0];
LFForm.onFieldChange(async (e) => {
  const count = LFForm.getFieldValues(charCount);
  console.log(count);
  await LFForm.changeFieldSettings(charCount, { subtext: `Character Count: ${count.length}` })
}, charCount)
 

0 0

Answer

SELECTED ANSWER
replied on December 6, 2024

I would use a regular expression on the single line field. For example, the following will require the use to enter exactly 5 characters:

^.{5}$

If they enter more or less than 5 characters, it throws an error. If you make the field required, it will also require them to enter at least 1 character before submitting.

2 0

Replies

replied on December 6, 2024

Just want to make sure I understand, but you are wanting the character count to always be X number of characters and not that they don't type more than X number of characters, right?

0 0
replied on December 6, 2024

Hi Blake,

     Correct, I have several fields that have a required number of characters. I would like to put their character count into a hidden field and then hide the Submit button if the conditions are not met. 

Thank You,

0 0
SELECTED ANSWER
replied on December 6, 2024

I would use a regular expression on the single line field. For example, the following will require the use to enter exactly 5 characters:

^.{5}$

If they enter more or less than 5 characters, it throws an error. If you make the field required, it will also require them to enter at least 1 character before submitting.

2 0
replied on December 6, 2024

Awesome! Thanks Blake, this worked perfectly. 

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.