Question T16763
Visible to All Users

Word Limits instead of Character Limits

created a year ago

Hi:

We have users who are interested in specifying a word limit rather than a character limit. Is this an option you would consider including in Survey.js? Our users build forms primarily for applications that specify word limits. For example, a proposal for funding to the United State's National Science Foundation often specifies that proposals can be no more than X number of words. Character count equivalents are hard to manage.

Thanks,

Adam

Answers approved by surveyjs Support

created a year ago

Hello Adam,
If you wish to validate user input and limit the number of words entered, you can implement the survey.onValidateQuestion function and validate the text in your JavaScript code. For more information, please visit Custom Form Validation Using an Event.

Consider the following demo: View Plunker.

JavaScript
const maxWordCount = 5; function validateText(text, maxWordCount) { text = text.trim(); let words = text.split(/\s+/); if (words.length > maxWordCount) { return false; } else { return true; } } survey.onValidateQuestion.add((sender, options) => { if(options.name === "question1"){ const textIsValid = validateText(options.value, maxWordCount); if(!textIsValid) { options.error = "Text exceeds the maximum word count"; } }

Please let me know if you have any further questions.

We hope you enjoyed the level of service you received. If so, we would love if you could leave your feedback on SurveyJS page at g2.com. As we’re building our business, every review helps build credibility for future customers and your feedback would be greatly appreciated.