I have a survey with many questions including two text fields. These two text fields must contain between 5 and 250 characters. Some users add a blank space to reach 5 characters. For example, they enter "Stop " then I have to modify their entry to meet the 5 character minimum requirement. I need to validate all data before submitting.
I'd like to trim white space and base the minimum character count on the resulting string. I've created a survey with some JavaScript that does this, but the JavaScript doesn't get called at the right time and the constraint doesn't work.
I've attached an XLSForm and a JavaScript file. (JSUtils.txt needs to be renamed to JSUtils.js.) Does anyone know how to get this to work? Thanks.
Solved! Go to Solution.
I found a solution that doesn't use JavaScript. I added a regular expression to the constraint to get the behavior that I'm looking for. Here's the whole constraint.
string-length(.)>=5 and string-length(.)<=250 and not(regex(., '(^[a-zA-Z0-9.-]{3,4}[ ]{1,2})|(^[ ]{1,2}[a-zA-Z0-9.-]{3,4})') and string-length(.) <= 6)
It looks complicated, but it simply checks for two patterns:
This essentially trims the white space.
I found a solution that doesn't use JavaScript. I added a regular expression to the constraint to get the behavior that I'm looking for. Here's the whole constraint.
string-length(.)>=5 and string-length(.)<=250 and not(regex(., '(^[a-zA-Z0-9.-]{3,4}[ ]{1,2})|(^[ ]{1,2}[a-zA-Z0-9.-]{3,4})') and string-length(.) <= 6)
It looks complicated, but it simply checks for two patterns:
This essentially trims the white space.