Max Text for a field

Hello,

I want to make my fields have a max number of characters and when they reach that number of characters the text is capped off and they can no longer enter anymore text. I have it in place that if they go more than 40 characters they get an error message but I would like it if it would just stop them from typing.
I can see online it looks like there are ways to do this with number and date fields but I am not seeing anything about a text field.

Is something like this possible?

Hello @Nazman1126,

Which type of the field are you using? Single line text, Plain text, or Rich text?

@mnikitina

This is for multi-line text fields and for my choice fields with other options.

I was able to do a single line text within SharePoint itself which gave me the idea to then do it for the rest of the fields that involve text input from the user.

Hello @Nazman1126,

You can use the code below to limit the length of the plain text field.

fd.spRendered(function() {

var previousValue = fd.field('Text').value;
fd.field('Text').$on('change', function(value) {
    if(value.length > 10) {
        fd.field('Text').value = previousValue
    }
    else {
        previousValue = value;
    }
});

});

You can use the same logic for the custom value input of the choice field.

fd.spRendered(function() {
var previousValue = fd.field('Choice').customValue
fd.field('Choice').$on('change', function(value) {
    if(fd.field('Choice').customValue.length > 10) {
        fd.field('Choice').customValue = previousValue
    }
    else {
        previousValue = fd.field('Choice').customValue;
    }
});

});

@mnikitina

Thank you for that. It was helpful. I am able to get one function to work though and I need to have 4 of them. I am not sure why only one will work at the other ones will not. These functions are in with other functions as well for the form. Below is the code. I see nothing wrong with it that only the first one will work and the others won't. Any insight you have would be appreciated. They are all written with the same syntax just different field names. 2 of these are for choice field and 2 are 4 multi-line text fields. The first one is a choice field and works fine. The other 2 doesn't give me any errors or break anything, they just don't work. At the bottom of all of my code is where the functions are called and same thing, written same syntax just different fields and function names.

image

Hello @Nazman1126,

Are you getting any errors in the browser console(F12)?

How do you call those functions? Share the complete code, please.