I have a textarea
<textarea id="text" name="text" rows="6"></textarea>
<style>
#text { width: 30%; }
</style>
When submitting the form, I need to check if the input exceeded 6 rows. I have a JS code, which checks for scrollbar (scrollbar present = more than 6 lines = invalid input) which works perfectly. The tricky part is, how can validate the input with PHP in case of disabled JS?
I tried something like this
public function overflowValidator($input, $maxLines, $cols) {
$linebreakCount = substr_count($input, "
");
$overflow1 = ($linebreakCount <= $maxLines);
$overflow2 = (ceil(strlen($input) / $cols) <= $maxLines);
return $overflow1 && $overflow2;
}
The biggest problem is that the width of the textarea is dynamic (responsive design) so I can't easily tell how many letters fit into one line. I'm afraid this can't be solved but maybe there is a solution I'm not aware of...
It might be a better idea to count the total amount of characters instead of lines but here is the code which allows you to do that :
preg_match_all("/(
)/", $_POST['text'], $matches);
$total_lines = count($matches[0]) + 1;
Count total lines :-
$lines = preg_split('/
|/',$str);
$Total_lines = count($lines);
echo $Total_lines;