Last active
April 5, 2021 18:00
-
-
Save rouaks/1af2b6174966b4f589d5d426d6f4496c to your computer and use it in GitHub Desktop.
Sample CKEditor5 JQuery validation plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form id="my_form_id"> | |
<textarea id="textarea" name="textarea" rows="6"></textarea> | |
<button type="submit">Submit</button> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$().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