Jquery Plugin: text input Character Count limiter (jquery. wordlimitor-1.0.1.js)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.