| 1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
| 2 |
<html xmlns="http://www.w3.org/1999/xhtml"> |
| 3 |
<head> |
| 4 |
<title>jQuery Validation plugin: integration with TinyMCE</title> |
| 5 |
|
| 6 |
<script type="text/javascript" src="../../lib/jquery.js"></script> |
| 7 |
<script type="text/javascript" src="../../jquery.validate.js"></script> |
| 8 |
<script type="text/javascript" src="tiny_mce.js"></script> |
| 9 |
<script type="text/javascript"> |
| 10 |
tinyMCE.init({ |
| 11 |
mode : "textareas", |
| 12 |
theme : "simple", |
| 13 |
// update validation status on change |
| 14 |
onchange_callback: function(editor) { |
| 15 |
tinyMCE.triggerSave(); |
| 16 |
$("#" + editor.id).valid(); |
| 17 |
} |
| 18 |
}); |
| 19 |
$(function() { |
| 20 |
var validator = $("#myform").submit(function() { |
| 21 |
// update underlying textarea before submit validation |
| 22 |
tinyMCE.triggerSave(); |
| 23 |
}).validate({ |
| 24 |
rules: { |
| 25 |
title: "required", |
| 26 |
content: "required" |
| 27 |
}, |
| 28 |
errorPlacement: function(label, element) { |
| 29 |
// position error label after generated textarea |
| 30 |
if (element.is("textarea")) { |
| 31 |
label.insertAfter(element.next()); |
| 32 |
} else { |
| 33 |
label.insertAfter(element) |
| 34 |
} |
| 35 |
} |
| 36 |
}); |
| 37 |
validator.focusInvalid = function() { |
| 38 |
// put focus on tinymce on submit validation |
| 39 |
if( this.settings.focusInvalid ) { |
| 40 |
try { |
| 41 |
var toFocus = $(this.findLastActive() || this.errorList.length && this.errorList[0].element || []); |
| 42 |
if (toFocus.is("textarea")) { |
| 43 |
tinyMCE.get(toFocus.attr("id")).focus(); |
| 44 |
} else { |
| 45 |
toFocus.filter(":visible").focus(); |
| 46 |
} |
| 47 |
} catch(e) { |
| 48 |
// ignore IE throwing errors when focusing hidden elements |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
}) |
| 53 |
</script> |
| 54 |
<!-- /TinyMCE --> |
| 55 |
|
| 56 |
</head> |
| 57 |
<body> |
| 58 |
|
| 59 |
<form id="myform" action=""> |
| 60 |
<h3>TinyMCE and Validation Plugin integration example</h3> |
| 61 |
|
| 62 |
<label>Some other field</label> |
| 63 |
<input name="title" /> |
| 64 |
|
| 65 |
<br/> |
| 66 |
|
| 67 |
<label>Some richt text</label> |
| 68 |
<textarea id="content" name="content" rows="15" cols="80" style="width: 80%"></textarea> |
| 69 |
|
| 70 |
<br /> |
| 71 |
<input type="submit" name="save" value="Submit" /> |
| 72 |
</form> |
| 73 |
|
| 74 |
</body> |
| 75 |
</html> |
| 76 |
|