I am using Quill Editor in angular 2, and want to limit the user to enter maximum 1000 char.
on('text-change', function (delta, old, source) { if (quill. getLength() > limit) { quill. deleteText(limit, quill. getLength()); } });
Quill Rich Text Editor Quill is a free, open source WYSIWYG editor built for the modern web. With its modular architecture and expressive API, it is completely customizable to fit any need.
You'll need to check manually if the editor's content is empty, prevent the user from submitting the form and show a message that this field is required. You can copy quill contents to a hidden input element before submitting the form as shown in this example. Save this answer. Show activity on this post.
There is as far as I know no built in configuration for that. You can however delete input that exceeds a certain length.
Example code from this github issue
const limit = 1000;
quill.on('text-change', function (delta, old, source) {
if (quill.getLength() > limit) {
quill.deleteText(limit, quill.getLength());
}
});
It is not clear if you are using pure quill or something like ngx-quill so I cannot provide a full example. Provide more details if you want additional help with integrating it in angular.
Remember to to quill.off(...)
for your text-change handler on ngOnDestroy
to prevent leaks.
Solution added using the ngx-quill module.
editor.component.ts
import { Component } from '@angular/core';
const MAX_LENGTH = 10;
@Component({
selector: 'editor',
templateUrl: './editor.component.html',
})
export class EditorComponent {
/*
* Delete added characters that exceeds max length
*/
textChanged($event) {
if ($event.editor.getLength() > MAX_LENGTH) {
$event.editor.deleteText(MAX_LENGTH, $event.editor.getLength());
}
}
}
editor.template.html
<quill-editor (onContentChanged)="textChanged($event)"></quill-editor>
Here is an example using ngx-quill.
One approach of doing this would be setting the maxLength to the desired number and show an error to the user. In addition, the action can be blocked until the user fixes it.
In here, I have added minLength and maxLength properties. It will display a message below the editor and disable the action button. Only when the validation is met, the button will be made active.
<quill-editor [style]="{height: '200px'}" name="notes"
[(ngModel)]="note" [minLength]="10" [maxLength]="400"
#noteInput="ngModel"></quill-editor>
<span class="hints" *ngIf="!noteInput.valid">
Min 10 characters and note more than 400</span>
<button fxFlexAlign="end" mat-raised-button color="primary"
class="btn--rounded" (click)="addNote()"
[disabled]="!ticketNoteInput.valid">Add</button>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With