I want to make textarea filed mandatory when I've applied TinyMCE.
If I add required
attribute to <textarea>
, it causes that I cannot submit the form even if I fill in the form!
How can I solve this problem?
tinymce.init({
selector: '#summaryId',
max_chars: 255, // max. allowed chars
plugins: "paste",
setup: function (ed) {
var allowedKeys = [8, 37, 38, 39, 40, 46]; // backspace, delete and cursor keys
ed.on('keydown', function (e) {
if (allowedKeys.indexOf(e.keyCode) != -1) return true;
if (tinymce_getContentLength() + 1 > this.settings.max_chars) {
e.preventDefault();
e.stopPropagation();
return false;
}
return true;
});
ed.on('keyup', function (e) {
tinymce_updateCharCounter(this, tinymce_getContentLength());
});
},
init_instance_callback: function () { // initialize counter div
$('#' + this.id).prev().append('<div class="char_count" style="text-align:left; color:grey;"></div>');
tinymce_updateCharCounter(this, tinymce_getContentLength());
},
paste_preprocess: function (plugin, args) {
var editor = tinymce.get(tinymce.activeEditor.id);
var len = editor.contentDocument.body.innerHTML.length;
var textLen = args.content.length;// $(args.content).text();
if (len + textLen > editor.settings.max_chars) {
alert('Pasting this exceeds the maximum allowed number of ' + editor.settings.max_chars + ' characters.');
args.content = '';
} else {
//tinymce_updateCharCounter(editor, len + text.length);
}
}
});
in cshtml file:
<textarea asp-for="Article.Summary" class="form-control" id="summaryId" maxlength="255"></textarea>