// Example for age validation
numeric_validation(
question_code = "Q1",
min_val = 1,
max_val = 99,
max_length = 3
);
Legacy code: The following codes works but have a drawback. If you type and click the next button fast, the text will not be properly deleted and we will recollect text instead numbers.
Version 1: Basic numeric open ended
One of the common case of use, is when you need to get numeric data from the respondent, i.e., to get the age, a quantity or an amount of money. This is when data validation comes to our part because SynoSurveys don't parse text to integers directly.
Affortunately, we have the following code to accept only numeric inputs in every single open ended question in the current page.
Warning: This code will remove strings on all the open ended questions that appear in the page. To be more specific, use the version 2 instead.
numeric_only.js
/*
NUMERIC ONLY - Version 1
Author: Kenneth DΓaz GonzΓ‘lez
Email: kdi@synoint.com
A snippet to allow that a textbox only receives numbers
This code will add this caracteristic for all the text inputs
that appear in the page. If you need to be more specific, use version 2
instead.
*/
// Get all the input texts
inputs = document.querySelectorAll(".custom-text");
// Iterate over them
inputs.forEach((input) => {
// If something has been written
input.addEventListener("keyup", (e) => {
// Replace the letters with empty strings
e.target.value = e.target.value.replace(/\D/g, "");
});
});
Version 2: Advanced numeric open ended
Sometimes, you will need to parse just one free answer to allow numeric values. In that case, you will need to use this code. To apply this parsing you need to pass the question code to the function when it's run.
/*
ADVANCED NUMERIC ONLY - Version 2
Author: Kenneth DΓaz GonzΓ‘lez
Email: kdi@synoint.com
A snippet to allow that a textbox only receives numbers
This code will add this caracteristic for a specified text input.
*/
function numeric_only(question_code) {
// Get all the input texts
input = document.querySelector("#p_" + question_code + "_1");
input.addEventListener("keyup", (e) => {
// Replace the letters with empty strings
e.target.value = e.target.value.replace(/\D/g, "");
});
}
// Call the function to apply it
numeric_only("Q1");