Validation
Forsta HX Platform - API Scripting Guide
Validation
Age Validation:
/**
* validateAge - Validates that the age input is within acceptable range
* @returns {boolean} - Returns true if age is valid (18-100), false otherwise
* @description Retrieves the age from question AQ1, parses it as integer,
* and checks if it falls within the valid range of 18-100 years.
* Displays an alert if validation fails.
*/
function validateAge() {
// Get the value from question AQ1 and convert to integer
// parseInt() converts string to number, returns NaN if invalid
var age = parseInt(f('AQ1').get());
if (isNaN(age) || age < 18 || age > 100) {
alert('Age must be 18-100');
return false;
}
return true;
}
Minimum Selections (Forsta API):
/**
* validateMinSelections - Ensures user has selected at least 2 options
* @returns {boolean} - Returns true if at least 2 options selected, false otherwise
* @description Uses Forsta API to loop through all possible options in BQ2,
* counts how many are selected using .toBoolean(), and validates
* that the minimum threshold of 2 selections is met.
*/
function validateMinSelections() {
// Get array of all possible option codes for question BQ2
var codes = f('BQ2').domainValues();
var count = 0;
codes.forEach(function(code) {
if (f('BQ2').item(code).toBoolean()) count++;
});
if (count < 2) {
alert('Select at least 2 options');
return false;
}
return true;
}
Date Range:
/**
* validateDates - Validates that end date comes after start date
* @returns {boolean} - Returns true if date range is valid, false otherwise
* @description Compares two date inputs to ensure logical ordering.
* Creates Date objects from the string values and compares them.
*/
function validateDates() {
// Create Date objects from the question values
// new Date() parses date strings into comparable Date objects
var start = new Date(f('CQ3').get());
var end = new Date(f('CQ4').get());
if (end <= start) {
alert('End date must be after start');
return false;
}
return true;
}
Real-time Validation:
f('DQ5').on('input', function() {
var val = f('DQ5').get();
if (val.length < 3) {
f('DQ5').addClass('error');
} else {
f('DQ5').removeClass('error');
}
});