Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<textarea id="description_edit" name="description_edit"></textarea>

Characters left:



<script type="text/javascript" src="/js/tinymce/tinymce.min.js"></script>
<script>
var max_chars = 200; //max characters
var max_for_html = 300; //max characters for html tags
var allowed_keys = [8, 13, 16, 17, 18, 20, 33, 34, 35,36, 37, 38, 39, 40, 46];
var chars_without_html = 0;
function alarmChars(){
if(chars_without_html > (max_chars - 25)){
$('#chars_left').css('color','red');
}else{
$('#chars_left').css('color','gray');
}
}
$(function() {
tinymce.init({
selector: "#description_edit",
theme: "modern",
width: 320,
height: 130,
plugins: [
"advlist autolink lists charmap print preview hr anchor pagebreak",
"searchreplace visualblocks visualchars code insertdatetime media nonbreaking",
"save table contextmenu directionality paste textcolor"
],
image_advtab: true,
language : 'en',
menubar:false,
statusbar:false,

setup : function(ed) {
ed.on("KeyDown", function(ed,evt) {
chars_without_html = $.trim(tinyMCE.activeEditor.getContent().replace(/(<([^>]+)>)/ig,"")).length;
chars_with_html = tinyMCE.activeEditor.getContent().length;
var key = ed.keyCode;

$('#chars_left').html(max_chars - chars_without_html);

if(allowed_keys.indexOf(key) != -1){
alarmChars();
return;
}

if (chars_with_html > (max_chars + max_for_html)){
ed.stopPropagation();
ed.preventDefault();
}else if (chars_without_html > max_chars-1 && key != 8 && key != 46){
alert('Characters limit!');
ed.stopPropagation();
ed.preventDefault();
}
alarmChars();
});
},

toolbar: "bold italic underline | alignleft aligncenter alignright alignjustify | forecolor backcolor | bullist numlist | charmap",
style_formats: [
{title: 'Bold text', inline: 'b'},
{title: 'Red text', inline: 'span', styles: {color: '#ff0000'}},
{title: 'Red header', block: 'h1', styles: {color: '#ff0000'}},
{title: 'Example 1', inline: 'span', classes: 'example1'},
{title: 'Example 2', inline: 'span', classes: 'example2'},
{title: 'Table styles'},
{title: 'Table row 1', selector: 'tr', classes: 'tablerow1'}
]
});

chars_without_html = $.trim($("#description_edit").text().replace(/(<([^>]+)>)/ig,"")).length;
$('#chars_left').html(max_chars - chars_without_html);
alarmChars();
});
</script>
Posted
Updated 10-Aug-14 21:54pm
v5
Comments
Sergey Alexandrovich Kryukov 11-Aug-14 3:41am    
Bad, really bad idea. Why?
—SA
Vikrant Chauhan 11-Aug-14 3:46am    
because i have tinymce in the admin end so whenever i put text in the admin end in the frontend it creates horizontal scrollbar,which i dont want ,i think width of the text line increases.
Vikrant Chauhan 11-Aug-14 3:55am    
http://stackoverflow.com/questions/11342921/limit-the-number-of-character-in-tinymce

see:---from top 4th no

1 solution

You can check this

how-to-break-line-after-some-characters-using-eval-in-asp-net-c-sharp[^]

Just replace the character.
Hope it will help..
 
Share this answer
 
Comments
Vikrant Chauhan 11-Aug-14 3:47am    
sir it is in php /tinymce

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900