Skip to content

Instantly share code, notes, and snippets.

@rouaks
Last active April 5, 2021 18:00
Show Gist options
  • Save rouaks/1af2b6174966b4f589d5d426d6f4496c to your computer and use it in GitHub Desktop.
Save rouaks/1af2b6174966b4f589d5d426d6f4496c to your computer and use it in GitHub Desktop.
Sample CKEditor5 JQuery validation plugin
<form id="my_form_id">
<textarea id="textarea" name="textarea" rows="6"></textarea>
<button type="submit">Submit</button>
</form>
$().ready(function() {
var editorTextarea
ClassicEditor.create( document.querySelector( '#textarea' ) )
.then (editor => {
editorTextarea = editor;
})
.catch( error => {
console.error( error );
} );
$ .validator.addMethod ("ckminlength", function (value, element, params) {
let content_length = editorTextarea.plugins.get( 'WordCount' );
return this.optional(element) || content_length.characters > params;
}, "Please enter {0} characters minimum.");
$("#my_form_id").validate({
ignore: [],
rules: {
textarea: {
required: true,
ckminlength: 10,
},
},
messages: {
textarea: {
required: "Your personnal text in your language",
ckminlength: "Your personnal text in your language"
}
},
submitHandler: function(form) {
// some other code
// maybe disabling submit button
// then:
form.submit();
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment