When facing the challenge of printing table content with unruly strings that lacked spaces for proper word wrapping, I encountered difficulties in maintaining consistent formatting. This led me to seek a solution for validating user input before reaching the print stage, as I wanted to avoid using CSS techniques that may not be fully supported by the print engine.
To address the issue of limiting contiguous characters in the text, I devised a method outlined below. While effective, I am still unsure if this is the best approach:
const limit = 25; // arbitrary threshold
/* text block possibly unformatted
without spaces for table alignment */
let str = some_user_input;
/* split text into array values using
any whitespace (added 'g' for safety) */
if(str.length){
let spaced = str.split(/\s+/g);
//verify array existence
if(spaced.length){
//check each array item for contiguous character limit
for(let i = 0; i < spaced.length; i++){
if(spaced[i].length > limit){
return false;
}
}//endLoop
}
else{
if(str.length > limit) return false;
}
}
return true;