Skip Navigation

[Resolved] Max characters multi line field

This thread is resolved. Here is a description of the problem and solution.

Problem:

How can I limit the maximum total count of characters or lines in a multi-line field in a form?

Solution:

Use the following jQuery code to limit the number of lines in a multi-line field. This example sets a limit of 2 lines, but you can adjust this by changing the value of maxLines:

jQuery.noConflict();
(function($) {
    $(document).ready(function() {
        var maxLines = 2;

        $('textarea').on('input', function() {
            var textarea = $(this);
            var lines = textarea.val().split('\n');

            if (lines.length > maxLines) {
                textarea.val(lines.slice(0, maxLines).join('\n'));
            }
        });
    });
})(jQuery);

Relevant Documentation:

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 7 replies, has 2 voices.

Last updated by Christopher Amirian 6 months, 2 weeks ago.

Assisted by: Christopher Amirian.

Author
Posts
#2708767

Hi,
How can I limit the max total count of characters or lines(preferable) in a multi line field? I found some older tickets and was doubting if they were up to date enough.

Thanks
Marco

#2708790

Christopher Amirian
Supporter

Languages: English (English )

Hi Marco,

I think it is possible via Javascript. May I have a page on your website with the multi-select so I can work on that to be able to see if I can provide a solution?

Thanks.

#2708890

Hi Christopher,

Thanks for your support! The form is in an user account so you have to login. I can send you some credentials. Please make the next message private.

Cheers
Marco

#2709006

Christopher Amirian
Supporter

Languages: English (English )

Hi Marco,

You can set the next reply as private and provide the login info and the URl that the form is there to check.

Thanks.

#2709026

Christopher Amirian
Supporter

Languages: English (English )

Hi Marco,

Thanks, I checked the video but unfortunately I could not enter the website as the password is not working.

Would you please double check with the info?

#2709167

Christopher Amirian
Supporter

Languages: English (English )

Hi Marco,

Thank you, the password worked.

Please add the jQuery code below:

jQuery.noConflict();
(function($) {
    $(document).ready(function() {
        var maxLines = 2;

        $('textarea').on('input', function() {
            var textarea = $(this);
            var lines = textarea.val().split('\n');

            if (lines.length > maxLines) {
                textarea.val(lines.slice(0, maxLines).join('\n'));
            }
        });
    });
})(jQuery);

Feel free to change "var maxLines = 2;" to whatever line number limitation you like.

The code above will limit the number of lines allowed to add content.

Thanks.

#2709177

nOw my client is telling me that some fields need 10 lines and others need 5 lines. Is this also possible?

#2709191

Christopher Amirian
Supporter

Languages: English (English )

Hi Marco,

In the code there is a selector of the text areas:

$('textarea')

You need to find a way to add some sort of class or ID to differentiate between the textareas and use one number for one and another number for the other one.

So if you have an HTML class added to the textarea like this:

textarea1
textarea2

Then you can have a code like this:

jQuery.noConflict();
(function($) {
    $(document).ready(function() {
        var maxLines1 = 2;
        var maxlines2 = 5
 
        $('.textarea1').on('input', function() {
            var textarea = $(this);
            var lines = textarea.val().split('\n');
 
            if (lines.length > maxLines1) {
                textarea.val(lines.slice(0, maxLines1).join('\n'));
            }
        });

        $('.textarea2').on('input', function() {
            var textarea = $(this);
            var lines = textarea.val().split('\n');
 
            if (lines.length > maxLines2) {
                textarea.val(lines.slice(0, maxLines2).join('\n'));
            }
        });


    });
})(jQuery);

Please consider that this will be considered as a custom development and we will not be able to fine-tune code for desire of your customer. If needed you are welcomed to hire a developer:

https://toolset.com/contractors/

Thank you.