From 18708ce46981d7f18d6bb3d92669d5468de6d81a Mon Sep 17 00:00:00 2001 From: DaniBitZesty <84323332+DaniBitZesty@users.noreply.github.com> Date: Mon, 15 Jul 2024 17:43:08 +0100 Subject: [PATCH] Fix word count when text is pasted and spaces are converted to to non-breaking spaces ( ). Also, count new lines as words. Change use of .context to .find('textarea') instead to fix step validation. --- app/assets/javascripts/frontend/form-validation.js.coffee | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/frontend/form-validation.js.coffee b/app/assets/javascripts/frontend/form-validation.js.coffee index f74d764e9..0a2bcc8a6 100644 --- a/app/assets/javascripts/frontend/form-validation.js.coffee +++ b/app/assets/javascripts/frontend/form-validation.js.coffee @@ -138,8 +138,10 @@ window.FormValidation = return question.find("input[type='checkbox']").filter(":checked").length validateWordLimit: (question) -> - wordLimit = $(question.context).attr("data-word-max") - wordCount = $(question.context).val().split(" ").length + textarea = question.find("textarea") + wordLimit = textarea.attr("data-word-max") + normalizedContent = textarea.val().replace(/ /g, ' ').replace(/\n/g, ' '); + wordCount = normalizedContent.split(" ").length if wordCount > wordLimit @logThis(question, "validateWordLimit", "Word limit exceeded") @addErrorMessage(question, "Exceeded #{wordLimit} word limit.")