The number of characters in the text input box that needs to be completed by the project is quite good. We recommend it to you.
1. Source Code
/*
* wordlimitor 1.0.1
* Date: 2011-08-01
* (c) 20011 gudufy,http://gudufy.cnblogs.com/
*
* This is licensed under the GNU LGPL, version 2.1 or later.
* For details, see: http://creativecommons.org/licenses/LGPL/2.1/
*/
$(function () {
$.fn.wordlimitor = function () {
$(this).each(function (i) {
if ($(this).nextAll('div.textlimit').size() == 0) {
$(this).after('<div class="textlimit">0</div>');
}
$(this).bind('propertychange focus keyup input paste', function () {
var _max = $(this).attr('max');
var _length = $(this).val().length;
if (_length > _max) {
$(this).val($(this).val().substring(0, _max));
}
_left = $(this).offset().left;
_top = $(this).offset().top;
_width = $(this).width();
_height = $(this).height();
$(this).nextAll('div.textlimit').html(_length + '/' + _max);
$(this).nextAll('div.textlimit').css({
'left': _left + _width + 15,
'top': _top + _height - 12
});
});
$(this).focus(function () {
$(this).nextAll('div.textlimit').fadeIn('slow');
});
$(this).blur(function () {
$(this).nextAll('div.textlimit').fadeOut('slow');
});
});
};
$('textarea[max],input[max]').wordlimitor();
});
2. Use(Introduce the wordlimitor file and enter max = "number of characters to be restricted" on the input control to be restricted)
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> character limiter </title>
<Style type = "text/CSS">
. Textlimit {
Position: absolute;
Font-size: 9pt;
Color: #4586b5;
Font-size: 16px;
Font-weight: bold;
Font-family: Arial;
Display: none;
}
</Style>
<SCRIPT src = "jquery-1.4.4.min.js" type = "text/JavaScript"> </SCRIPT>
<SCRIPT src = "jquery. wordlimitor-1.0.1.js" type = "text/JavaScript"> </SCRIPT>
</Head>
<Body>
<P> Single Row restriction: <input type = "text" size = "40" max = "50"/> </P>
<P> multi-row restriction: <textarea rows = "4" Cols = "40" max = "200"> </textarea> </P>
<Br/>
<A href = "mailto: gudufy@163.com"> suggestion </a>
</Body>
</Html>
3. Download
Click to download