dongshou9343 2013-09-11 12:20
浏览 684

计算textarea中的行数,包括自动换行符

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...

  • 写回答

2条回答 默认 最新

  • doushou6480 2013-09-11 18:17
    关注

    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;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 CSS实现渐隐虚线边框
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题