Select to view content in your preferred language

How to trim white space in a text field?

175
1
Jump to solution
4 weeks ago
ChuckS
by
New Contributor III

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.

0 Kudos
1 Solution

Accepted Solutions
ChuckS
by
New Contributor III

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:

  • 3 or 4 characters followed by 1 or 2 spaces.
  • 1 or 2 spaces followed by 3 or 4 characters.

This essentially trims the white space.

 

View solution in original post

0 Kudos
1 Reply
ChuckS
by
New Contributor III

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:

  • 3 or 4 characters followed by 1 or 2 spaces.
  • 1 or 2 spaces followed by 3 or 4 characters.

This essentially trims the white space.

 

0 Kudos