0

How can we limit the number of characters entered in rich text field lightning field

<lightning-input-rich-text
        value={myVal}>
    </lightning-input-rich-text>

Is there any provision to provide maxlength so that user cannot enter anything more than specified number of characters; I dont see anything in the documentation https://developer.salesforce.com/docs/component-library/bundle/lightning-input-rich-text/specification

Any help will be appreciated

1 Answer 1

0

As you said, there is no attribute for what you want. Within that same documentation is the following

lightning-input-rich-text doesn't provide built-in validation but you can wire up your own validation. Set the valid attribute to false to change the border color of the rich text editor to red. Set the required and label-visible attributes to display the asterisk near the label.

So you'll have to wire up your own logic to check the length of the value within the input

<template>
    <lightning-input-rich-text
        value={myVal}
        message-when-bad-input={errorMessage}
        valid={validity}>
    </lightning-input-rich-text>
</template>

within your js, you can do it on a button (if it's to be submitted) or on the change onchange within the input

onSubmit(){
    if(this.myVal.length > 300){
        this.validity = false;
        this.errorMessage = "You have exceeded the max length";
    } else {
        //submit
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .