Limit Characters in Text Field

Hello! We are using the Plumsail Public Forms and are trying to find a way to limit the number of characters in a multi-line text box to 3,000 characters. Has anyone done this? I know the attribute for HTML inputs, but I am not sure how that would be used in the designer.

Thanks!

Dear @kkreitzer,
Sure, we can help, but we might need a little more details to make it work with your situations specifically:

  • Do you want users to be able to input more than 3,000 characters, but then not being able to save the form, like validation? Or do you want some hard limit, when users are not even able to type in more than 3,000 characters?

  • Is this Multiline Field Plain Text or Rich Text? The value returned would be different, and if it’s Rich Text, there needs to be additional code to remove all the styling from the value.

  • Finally, do you want to include spaces as characters, or should only actual visible characters count?

In case you have a simple Plain Text Multiline Field, you can use the following code for validation, though it will include spaces as well:

fd.spRendered(function() {
	fd.validators.push({
	        name: 'FieldLengthValidator',
	        error: "FIELD_NAME must be not longer than 3000 characters",
	        validate: function() {
	            if (fd.field('FIELD_NAME').value.length <= 3000) {
	                    return true;
	                }
	            return false;
	    }
	});
});

Hi Nikita.
I've tried this exact code on a public form, changing the value to 600. However it doesn't prevent long submissions. Is there more code that I need in JS than this?
Below is exactly what I've entered. 'Instructions' is the internal name of the field.

fd.spRendered(function() {
fd.validators.push({
name: 'FieldLengthValidator',
error: "Instructions must be not longer than 600 characters",
validate: function() {
if (fd.field('Instructions').value.length <= 600) {
return true;
}
return false;
}
});
});

Hello @duncanw,

fd.spRendered is an event that used in SharePoint forms. For public forms please use fd.rendered event.

Please find more information about events for PUblic Forms here.

1 Like

Hi, I need the solution for entering hard limits where the users will not be able to type in more than the character limit of 3,000. These limit should include spaces and punctuation. I need this for both Plain and Rich multiline text boxes.

I also have some situations where the limit is based on words, for example 200 word limit in multiline text boxes. Is that possible to have as hard limits as well.

Thank you.

Hello @Zusbry,

To hard limit the number of characters in the Plain text field, you can use this code:

   text = fd.field('FIELD_NAME').value;

   function limitCharacters(fieldName){
       var newText = fd.field(fieldName).value;
       if(fd.field(fieldName).value.length > 3000) {
            fd.field(fieldName).value = text;
       }
       else {
           text = newText;
       }
   } 

   fd.field('FIELD_NAME').$on('change', function(value) {
       limitCharacters('FIELD_NAME');
   });

If you need help with limiting the number of character in the Rich text field and setting the limit on word number, we can offer you a paid support. Please contact us via [email protected]

2 Likes

Hello @mnikitina,

Thank you, I really do appreciate you taking the time. It works perfectly. I will followup on the decision for Rich text.

Thanks again.

1 Like

Hi @mnikitina and @Nikita_Kurguzov.

What would the plain text validation be for restricting the following invalid characters in SharePoint?

~ " # % & * : < > ? / \ { | }.

Thanks,
stormanh

Hello @stormanh,

You can use the code below to validate the plain text field:

fd.spRendered(function() {
    fd.field('Title').validators.push({
    name: 'Invalid characters',
    error: "Please remove invalid characters",
    validate: function() {
    	var str = fd.field('Title').value;
    	return !/[~"#%&*:<>?/\{|}.]/g.test(str);
    }
    });
});

Thank you @mnikitina!

1 Like