JQuery maxlength Plugin-determine input field character length

Source: Internet
Author: User
Tags diff split
The official address of the plugin http://remysharp.com/2008/06/30/maxlength-plugin/
The call is simple:
<input type= "text" name= "value=" "id=" J-text "maxlength=" "/><span> The remaining words: <em id=" J-tip-text "> </em></span>
<script src= "Jquery1.4.4.js" type= "text/Web Effects" charset= "Utf-8" ></script>
<script src= "Maxlength.js" type= "Text/javascript" charset= "Utf-8" ></script>
<script type= "Text/javascript" charset= "Utf-8" >
$ (' #j-text '). MaxLength (' #j-tip-text ');
</script>
</body>
But it's a lot of trouble to write, and the most painful thing is to verify that the outer edge of the input field must have a form label. So think of the transformation, the best is directly in the input to write maxlength attributes, do not go to the best page calls.
So I inserted this paragraph:
var $tip = $ (' <span> remaining words:<em></em></span> ');

var $charsLeft = $tip. Find (' em ');

$tip. InsertAfter ($field);
This disables the JS case will not appear no use of the hint, not enough to feel better should be fixed length instructions written in the back, and then use JS Remove, put on a dynamic hint.
An instance:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<style type= "Text/css Tutorial" >
form{border-top:1px solid #e4e4e4;}
Label{display:block;}
Form span{Display:block;}
</style>
<body>
<form action= "#" method= "Get" accept-charset= "Utf-8" >
<p><label for= "" >input:</label>
<input type= "text" name= "value=" "id=" J-text "maxlength=" "/>"
<span> remaining words: <em id= "J-tip-text" ></em></span></p>
<p><label for= "" >textarea:</label>
<textarea name= "Name" rows= "3" cols= "id=" J-area "></textarea>"
<span> remaining words: <em id= "J-tip-area" ></em></span>
<input type= "hidden" name= "maxlength" value= "id=" j-area-hide
<p><label for= "" >by word:</label>
<input type= "text" name= "value=" "id=" J-word "maxlength=" "/>"
<span> remaining words: <em id= "J-tip-word" ></em></span></p>
</form>
<form action= "#" method= "Get" accept-charset= "Utf-8" >
<p><label for= "" >input:</label>
<input class= "maxlength" type= "text" name= "" value= "" id= "j-text-1" maxlength= ""/></p> "
<p><label for= "" >textarea:</label>
<textarea class= "maxlength" name= "name" rows= "3" cols= "id=" "J-area-1" maxlength= "></textarea>
<input type= "hidden" name= "maxlength" value= "id=" j-area-hide
</form>
<script src= "Jquery1.4.4.js" type= "Text/javascript" charset= "Utf-8" ></script>
<script src= "Maxlength.js" type= "Text/javascript" charset= "Utf-8" ></script>
<script type= "Text/javascript" charset= "Utf-8" >
$ (' #j-text '). MaxLength (' #j-tip-text ');
$ (' #j-area '). MaxLength (' #j-tip-area ');
$ (' #j-word '). MaxLength ({
' Feedback ': ' #j-tip-word ',
' Words ': true
});
$ ('. MaxLength '). MaxLength ();
</script>
</body>
MaxLength plug-in instructions, just identify the changes to the place, and then a good change:
(function ($) {

$.fn.maxlength = function (settings) {
If there is only one argument, the default is the container for the actual result
if (typeof settings = = ' String ') {
Settings = {Feedback:settings};
}
Configuration parameters, missing default substitution
Settings = $.extend ({}, $.fn.maxlength.defaults, settings);
Get the number of characters/words in the input field
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),
It's a bad idea. The perimeter must have a form tag
$form = $ (field.form),
Limit = Settings.useinput? $form. Find (' input[name=maxlength] '). Val (): $field. attr (' maxlength '),
$charsLeft = $form. Find (Settings.feedback);

Added by
var $tip = $ (' <span> remaining words:<em></em></span> ');
var $charsLeft = $tip. Find (' em ');
$tip. InsertAfter ($field);

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 () {
var len = length (field),
diff = Limit-len;

$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 ();
});
};
Here you can add a separation, with a custom delimiter, such as "," or something.
$.fn.maxlength.defaults = {
Useinput:false,
Hardlimit:true,
Feedback: '. Charsleft ',
Words:false
};

}) (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.