Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

osherdo's avatar

How do I add maxlength to blade templating

Hi.

I have a textarea generated by blade engine.

I need to limit the character length for it. How do I do it please?

code:

{!! Form::textarea('notes', 'By default,other users can see your goals.',['class'=>'form-control']) !!}</p> 

EDIT:

I tried this:

{!! Form::textarea('notes', 'By default,other users can see your goals.',['class'=>'form-control'],array('placeholder' => '','maxlength' => 10 )) !!}</p> 

still no success there.

0 likes
10 replies
d3xt3r's avatar

@osherdo You can add whatever attribute you want in the array.

 {!! Form::textarea('notes', 'By default,other users can see your goals.',['class'=>'form-control' , 'maxlength' => 100, 'random' =>'attribute']) !!}

will result in

<textarea class="form-control" maxlength="100" random="attribute" name="notes" cols="50" rows="10">By default,other users can see your goals.</textarea>
ChristopherSFSD's avatar

If you are limiting characters based on maximum database field size, it gets a little more complicated.

What I have chosen to do is alert the user via JavaScript when they have exceeded the maximum character limit based on how much space each character requires in the database. I also require users of my app to have JavaScript enabled.

I'm not 100% certain I'm doing this completely right so if someone spots something please tag me.

function getUTF8Length(string)
{
    var utf8length = 0;

    for (var n = 0; n < string.length; n++)
    {
        var c = string.charCodeAt(n);

        if (c < 128) utf8length++;
        else if((c > 127) && (c < 2048)) utf8length = utf8length+2;
        else utf8length = utf8length+3;
    }

    return utf8length;
}

The code that consumes the above function ...

    // TEXTAREA INPUTS - ALERT WHEN MAX LENGTH EXCEEDED:
    $(document).on('keyup', 'textarea', function(e)
    {
        var max_length = 65393;
        var length = getUTF8Length(this.value);
        var status_div = $(this).parent().find('div.status');

        if (status_div)
        {
            $(this).parent().removeClass('error').removeClass('with_status');
            $(status_div).removeClass('field_errors');

            if (max_length == length)
            {
                $(this).parent().addClass('with_status');
                $(status_div).html('<ul><li>Character limit reached.</li></ul>');
            }
            else if (max_length < length)
            {
                var percent = Math.floor(((length / max_length) * 100) - 100);
                var percent_text = (percent > 1) ? ' by approximately ' + percent + '%' : ' by less than 1%';

                $(this).parent().addClass('error').addClass('with_status');
                $(status_div).addClass('field_errors');
                $(status_div).html('<ul><li>Character limit exceeded' + percent_text + '.</li><li>Characters in excess of the limit will will not be stored.</li></ul>');
            }
            else
            {
                $(status_div).html('');
            }
        }
    });
osherdo's avatar

@premsaurav unfortunately this is not working and the when I am typing in the text area and reaching 100 characters it does not stop.

{!! Form::textarea('notes', 'By default,other users can see your goals.',['class'=>'form-control'],['maxlength'=>100]) !!}</p> 

any idea please?

osherdo's avatar

@premsaurav I am checking this on Ubuntu's Google Chrome.

I think the code is good since it does not show any errors when running the view.

I will try using Jquery then I believe.

thomaskim's avatar

@osherdo You didn't do it properly. @premsaurav said to pass it in the array, not to pass two arrays.

{!! Form::textarea('notes', 'By default,other users can see your goals.',['class'=>'form-control', 'maxlength'=>100]) !!}</p> 
osherdo's avatar

@thomaskim thanks for that! It is working indeed! my mistake for not reading the code thoughtfully enough.

I also understand that I shouldn't create more than one array for each element.

I get it now. Thanks really.

1 like
jekinney's avatar

Use html instead of form helpers. Better performance and much easier. Why rum php functions just to out put basic html?

Please or to participate in this conversation.