Counting respondent key strokes against total character count using JavaScript on an open question
What does this feature accomplish?
This script can help survey programmers ensure integrity of respondent data (AKA 'fraud prevention') by comparing the number of characters in open-ended questions versus the number of total keystrokes a respondent has performed. This can help filter out 'cheaters' who use copy/paste to fill out verbatim questions.
To see this functionality in action, click this link to our example survey.
The Script
Implementing this script is straightforward, but also requires customization to function as intended when incorporated into your own specific survey. Insert the following JavaScript on the screen you wish to enable this function as a label.
<script type="text/javascript">
var total = 0;
var idqName = '!!q1.InputName()!!'
var idkCount = '!!KeyPressCount.InputName()!!'
var idcCount = '!!CharacterCount.InputName()!!'
document.addEventListener("DOMContentLoaded", () => {
var el = document.querySelector('[name="other' + idqName + '"]');
var n1 = document.querySelector('[name="' + idkCount + '"]');
var n2 = document.querySelector('[name="' + idcCount + '"]');
el.addEventListener('keyup', (e) => {
total++;
n1.value = total;
n2.value = el.value.length;
})
el.addEventListener('change', (e) => {
n2.value = el.value.length;
})
});
</script>
- q1 = your verbatim question to be shown to the respondent.
- KeyPressCount = hidden numeric variable that counts the total keystrokes submitted into q1.
- CharacterCount = hidden numeric variable that counts the overall length of characters submitted into q1.
Implementation
Once the script is written and inserted as a label on the screen, you must merge the previous three questions onto the same screen. These questions must share the same screen for the script to work correctly. In order to hide both the KeyPressCount and CharacterCount inputs from respondents, you can insert the following script into their 'code in line <TR>' HTML settings in element properties, ensuring to do the same for the variable's long caption label:
style='display:none'
Final appearance:
An example QEX file has been attached to this article for quick reference and implementation.\
Credit:
Many thanks to Jack Wood, at Consumer Insight, who came up with this neat trick to help spot obvious cheaters using copy and paste in open questions - and for allowing us to share this with other Askia clients. Jack is an absolute star.