Skip to content

Commit

Permalink
Fix word count when text is pasted and spaces are converted to to non…
Browse files Browse the repository at this point in the history
…-breaking spaces ( ).

Also, count new lines as words.
Change use of .context to .find('textarea') instead to fix step validation.
  • Loading branch information
DaniBitZesty committed Jul 15, 2024
1 parent 3882a99 commit 18708ce
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/assets/javascripts/frontend/form-validation.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down

0 comments on commit 18708ce

Please sign in to comment.