Skip Navigation

[Resolved] Max Character length on custom "Multiple Lines" field from a post field group.

This support ticket is created 3 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 11 replies, has 2 voices.

Last updated by julesW 3 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#2203337

I would like to add a max character length to a custom "Multiple Lines" field from a post field group.

I've reviewed https://toolset.com/forums/topic/limit-count-characters-in-text-area-for-post-type-form/#post-1289503

It's unclear to me where I place the code bit below. Is this something I'm supposed to add to my function.php file?

jQuery(document).ready(function ($) {
$('textarea[name="wpcf-litter-puppy-description"]').attr('maxlength',200);

});

Thanks for the clarification.
Jules

#2204733

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Do you mean that when you add/edit the post from the backend you want to restrict your multiline field to maximum number of characgters you define? If this is correct:
- There is no such native feature available. You will have to add custom JS codes that runs on backend.

Can you please tell me what field you want to restrict with what number of characters?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2205907

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The thing is that you are targeting the custom field that is added to the repeating field group and when you try to add a new entry for the repeating field group it will be added via AJAX and it will require notable custom Javascript/jQuery code to limit the field you are saying and that is beyond the scope of our support policy.

As it requires the custom code and for that you will have to either adjust the code or you are welcome to contact our certified partners and they will help you:
=> https://toolset.com/contractors/

However - here are few links that may help you:
(Please note that its just as a reference and you will require to adjust the custom code as per your requirement).
- https://toolset.com/forums/topic/limit-the-number-of-words-or-characters-in-editor/
- https://toolset.com/forums/topic/custom-field-character-limit-in-custom-post-type-on-back-end/

#2206487
toolset_maxchar_length_fieldgroup.png
toolset_maxchar_length.png

Hi Minesh

Thank you for the information and resource links.

I've added the function and the jQuery is presenting in the source code, but id doesn't seem to be working.

Below is the code I'm using and attached is a screenshot showing that the script is presenting in the source code of the edit entry page.

<?php
add_action( 'admin_print_footer_scripts', 'limit_characters_puppy_description', 100 );
 
function limit_characters_puppy_description() {
    global $parent_file;
    if( 'edit.php?post_type=litters' == $parent_file ) {
        ?>
        <script type="text/javascript">
            var editor_char_limit = 50;
    
            // jQuery ready fires too early, use window.onload instead
            window.onload = function () {
                jQuery('[name="wpcf[litter-puppy-description]"]').after('<span class="word-count-message">Reduce word count!</span>');
                jQuery('[name="wpcf[litter-puppy-description]"]').keypress(function(e) {
                    var tval = jQuery('[name="wpcf[litter-puppy-description]"]').val(),
                        tlength = tval.length,
                        set = 100,
                        remain = parseInt(set - tlength);
                        jQuery(".word-count-message").text(remain);
                    //$('p').text(remain);
                    if (remain <= 0 && e.which !== 0 && e.charCode !== 0) {
                        jQuery('[name="wpcf[litter-puppy-description]"]').val((tval).substring(0, tlength - 1))
                    }
                })
            }
        </script>
    
        <style type="text/css">
            .word-count-message { font-size:1.1em; display:block; float:right; color:black; font-weight:bold; margin-top:2px; }
        </style>
        <?php
    }
}

I thought this might be because the text area I'm targeting is in "Repeatable Group" field so I tested stripping down the function to just adding "HELLO there" after any textarea field. Though it presented in the source code there was no "HELLO there" rendered on the edit entry page. Below is the code bit I tested.

I attached a second screenshot showing the field group on the edit entry page.

add_action( 'admin_print_footer_scripts', 'limit_characters_puppy_description', 100 );
 
function limit_characters_puppy_description() {
	global $parent_file;
    if( 'edit.php?post_type=litters' == $parent_file ) {
		?>
		<script type="text/javascript">			
			window.onload = function () {			
				jQuery('textarea').after("HELLO there");
			}				
		</script>
		
    <?php
	}
}

Can you see what I'm doing incorrectly? Is this something you can assist with?

Thank you for your time!
Jules

#2206633

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

I've adjusted the custom code and added the snippet as given under:
=> hidden link

I can see it works as expected. Can you please confirm it works at your end as well.

#2206965
toolset_maxchar_length_fieldgroup_2.png

Hi Minesh

Thank your continued assistance!

I can see the snippet you added, but I'm not seeing any word-count-message on the [litter-puppy-description] field.

I'm seeing an error in the console and am wondering if that is part of the issue. I've attached a screenshot so you can see what I'm seeing.

#2207025

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - as this is a repeating field group and there will me multiple instance available so the best and simplest solution would be to add maxlength for the text area.

I've adjusted the code as given under and I can see its adding maxlength with 200 so user can not add more than 200 characters. You just simply add a notice to your custom filed that only 200 characters allowed. That I already set from the custom field group "Only 200 characters allowed.":
=> hidden link

add_action( 'admin_print_footer_scripts', 'limit_characters_puppy_description', 100 );
 
function limit_characters_puppy_description() {

    global $parent_file;
    if( 'edit.php?post_type=litters' == $parent_file ) {
        ?>
        <script type="text/javascript">
            var editor_char_limit = 200;
    
            // jQuery ready fires too early, use window.onload instead
            window.onload = function () {
				jQuery('[name*="[litter-puppy-description]"]').prop("maxLength",200);
			}
        </script>
    
         
        <?php
    }
}
#2207049

Hi Minesh

I see the "Only 200 characters allowed." description added to the Multiple Lins field and I see the code snippet has been updated.

But, I'm still able to add, save, and view more than 200 characters.

#2207055

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Please make sure that you hard clear your browser cache and then you should check. I can see its working as expected and maxlength attribute is added to textarea.

#2207095
toolset_limit-characters-in-description-field_2.png

Hi Minesh

I'm wondering if what we expect is different.

I've cleared my browser cache. I can see the code you updated in the source code. I do not see any character limitation other than the custom field description text. I don't see any evidence that the code snippet is impacting the textfield. I'm not sure if I'm missing something or if we have different expectations.

I've attached a screenshot so you can see what I'm seeing.

Thank you for your patience and support.
Jules

#2207121

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check with the blank field?

Please check the following video: hidden link

Its working for me. As this is a custom code and I also guided you how you can achieve this. This is already beyond the scope of our support policy. You are welcome to contact the pro Javascript expert to get help for such custom edits.

#2209323

Hi Minesh

I took your advice and found someone on upwork to assist me. I'm sharing the solutuion so that others can benefit from it.

I think the main challenge is becuase the toolset repeatable fields within the field group are added dynamically, making it hard to target onload. To help a jQuery document on focus was added. Brandon Adderley @ upwork is the author of the code bit below

add_action( 'admin_print_footer_scripts', 'limit_characters_puppy_description', 100 );
 
function limit_characters_puppy_description() {
	
	// Run code on edit posts of custom post type "litters"
    global $parent_file;
    if( 'edit.php?post_type=litters' == $parent_file ) {
        ?>
        <script type="text/javascript">
            var editor_char_limit = 200;
    
            // jQuery ready fires too early, use window.onload instead
            window.onload = function () {								
				
				jQuery(document).on('focus', '[name*=litter-puppy-description]', function() {
					
				  var textlen = editor_char_limit - jQuery(this).val().length;
					
				  var id = jQuery(this).attr('id');          
				  var descid = id + "-maxlength";
				  var maxlengthpara = jQuery(this).next('p');
					
					jQuery(this).prop("maxLength",editor_char_limit);
					
				  if (!maxlengthpara.length) {   
					  var newparagraph = "<p id='" + descid + "' class='my-class'> <span>" + textlen + " </span> Character(s) Remaining </p>";
					  jQuery(this).after(newparagraph);					  					  
				  }					
				});
				
				jQuery(document).on('keyup', '[name*=litter-puppy-description]', function() {
				  var textlen = editor_char_limit - jQuery(this).val().length;

				  var id = jQuery(this).attr('id');          
				  var descid = id + "-maxlength";
				  var maxlengthpara = jQuery(this).next('p');

				  if (maxlengthpara.length) {   					  
					  maxlengthpara.find('span').html(textlen);					  
				  } 				  					
				});
												
			}		
	
        </script>    
         
        <?php
    }
}

Thank you for your time, patience, and help!