Different maxlength validation of textarea with newlines in Chrome and Firefox

后端 未结 1 1922
没有蜡笔的小新
没有蜡笔的小新 2021-01-18 09:34

The problem is that Firefox counts newline as \"1\"(\\n) character while Chrome count them as \"2\"(\\r\\n) This is what I get in a t

相关标签:
1条回答
  • 2021-01-18 10:03

    I didnt find any solution to make the maxlength atribute to work the same way in all browsers, and I think is because its relative new. So I decided to go for javascript.

    Googling a little I found this jQuery Max Length plugin that works great just adding a few lines. Is available under the MIT licence. And counts newlines as it should be (2 characters)

    It has some parameters to set the maxlength, feedback text, background change when full, between others. The website has very good documentation and a lot of examples.

    Example

    It's highly customisable‎, for example here is mine

    Own

    This is the only code I used plus some css.

    <script type="text/javascript" src="jquery.plugin.js"></script>
    <script type="text/javascript" src="jquery.maxlength.js"></script>
    <script>
        $(function() {
            $('.t_tresc').maxlength({
                showFeedback: true,
                feedbackText: '{c} / {m} max.', 
                overflowText: '{c} / {m} max!', 
                feedbackTarget: '#targetFeedback$name',
                max: $max_length,
                truncate: false,
                onFull: null
            }); 
        });
    </script>
    
    0 讨论(0)
提交回复
热议问题