jquery calculated text box input character number code

Source: Internet
Author: User
Tags diff

jquery calculated text box input character number code
ASP tutorial X Files:

Introduce JS

<script type= "text/page special Effects" src= ". /script/maxlength.js "></script>

Because the textarea control is restricted, a hidden input must be used to control the length, because the TextArea MaxLength property takes the value of the character length that the user actually entered. If you are restricting input length, you do not need.

<asp:textbox textmode= "Multiline" wrap= "true" runat= "Server" id= "Positioninfo" rows= "width=" 825px "maxlength=" 3000 "></asp:textbox>
<br/> Up to 3000 words, can also enter <span id= "Descsize" >3000</span> Word
<input type= "hidden" name= "maxlength" value= "3000" id= "MaxLength"/>

jquery References:

<script type= "Text/javascript" >
$ (document). Ready (function () {

$ (' #<%=positioninfo.clientid%> '). Bind (' PropertyChange ', function () {
$ (this). MaxLength ({
' Feedback ': ' descsize ', ' useinput ': true
});
});

});
</script>

jquery Code

(function ($) {

$.fn.maxlength = function (settings) {
if (typeof settings = = ' String ') {
Settings = {Feedback:settings};
}

Settings = $.extend ({}, $.fn.maxlength.defaults, settings);

function Length (EL) {
var parts = El.value;
if (settings.words)
Parts = el.value.length? Parts.split (/s+/): {length:0};
return parts.length;
}

Return This.each (function () {
var field = this,
$field = $ (field),
$form = $ (field.form),
Limit = Settings.useinput? $form. Find (' # ' +settings.maxlength). Val (): $field. attr (' maxlength '),
$charsleft = $form. Find (' # ' +settings.feedback);

function Limitcheck (event) {
var len = length (this),
exceeded = Len >= limit,
Code = Event.keycode;

if (!exceeded)
Return

Switch (code) {
Case 8://Allow Delete
Case 9:
Case 17:
Case://Cursor keys
Case 35:
Case 37:
Case 38:
Case 39:
Case 40:
Case 46:
Case 65:
Return

Default
Return settings.words && code!= && code!= && len = = limit;
}
}


        var updatecount = function () {
     & nbsp;      var len = length (field),
              diff = limit-len;        
   & nbsp;        $charsleft. html (diff | | "0");

Truncation Code
if (Settings.hardlimit && diff < 0) {
Field.value = settings.words?
Split by white spaces, capturing it in the result, then glue them
Field.value.split (/(S+)/, (limit*2)-1). Join ("):
Field.value.substr (0, limit);

Updatecount ();
}
};

$field. KeyUp (Updatecount). Change (Updatecount);
if (settings.hardlimit) {
$field. KeyDown (Limitcheck);
}

Updatecount ();
});
};

$.fn.maxlength.defaults = {
Useinput:false,
Hardlimit:true,
Feedback: ' Charsleft ',
Words:false,
MaxLength: ' MaxLength '
};

}) (jquery);

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.