I am using jQuery to validate forms.. but when I use CKeditor and try to validate it using jQuery, it's not working.
Here is the snippet of HTML code
<form class="form-horizontal" role="form" name="f3" id="f3" >
<div class="col-xs-8">
<textarea class="ckeditor" name="cktext" id="cktext"></textarea>
</div>
<button type="submit" class="btn btn-default btn-success">Submit</button>
</form>
Here is the form validation code
<script>
$(document).ready(function(){
$("#f3").validate(
{
debug: false,
rules: {
cktext: {
required: true,
minlength: 10
}
}
});
});
</script>
FYI : jQuery validation working for other form fields expect the ckeditor textarea field
Any suggestions.. to get rid of this problem..
Finally i found the answer to my question...
I changed the value of ignore property which by default holds :hidden value. as CKEDITOR hides the textarea jQuery validation doesn't validate the element:
ignore: []
Just i changed the validation script as follows..
$(document).ready(function(){
$("#f3").validate(
{
ignore: [],
debug: false,
rules: {
cktext:{
required: function()
{
CKEDITOR.instances.cktext.updateElement();
},
minlength:10
}
},
messages:
{
cktext:{
required:"Please enter Text",
minlength:"Please enter 10 characters"
}
}
});
});
HTML snippet is
<form class="form-horizontal" role="form" name="f3" id="f3" >
<div class="col-xs-8">
<textarea class="ckeditor" name="cktext" id="cktext"></textarea>
</div>
<button type="submit" class="btn btn-default btn-success">Submit</button>
</form>
As i found this answer in Here
Thanks to all...
I took the previous answer and fleshed it out with an actual CKEditor, so that you can see what needs to be done to copy the contents of the CKEditor into your textarea before submit.
The key bits are this:
CKEDITOR.on('instanceReady', function () {
$.each(CKEDITOR.instances, function (instance) {
CKEDITOR.instances[instance].document.on("keyup", CK_jQ);
CKEDITOR.instances[instance].document.on("paste", CK_jQ);
CKEDITOR.instances[instance].document.on("keypress", CK_jQ);
CKEDITOR.instances[instance].document.on("blur", CK_jQ);
CKEDITOR.instances[instance].document.on("change", CK_jQ);
});
});
function CK_jQ() {
for (instance in CKEDITOR.instances) {
CKEDITOR.instances[instance].updateElement();
}
}
Which I got from this answer to a different but similar question.
The other error you have is misspelling minlength
in your rules object.
This is what it looks like working: http://jsfiddle.net/ryleyb/QcJ57/
jQuery.validator.setDefaults({
ignore: [],
// with this no hidden fields will be ignored E.g. ckEditor text-area
});
I observed the validation was working on 2nd submit. The reason is, ckEditor
hides the actual text area and puts an iframe as an editor instance, and on submit it pushes the content to the text area. Which means, the validation on the TextArea
gets fired on stale data. To fix this problem, I am updating my TextArea
on the text change of the editor instance.
for (instance in CKEDITOR.instances) {
CKEDITOR.instances[instance].on('change', function ()
{
var editorName = $(this)[0].name;
CKEDITOR.instances[editorName].updateElement();
});
}
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