jquery determines the number of characters to enter (the length of the digital English is 1, Chinese is 2, more than the length of automatic interception) _jquery

Source: Internet
Author: User
1. $ (' textarea#txtprizenote '); Represents the name of the textarea control
2. ' Span ' shows the label of the remaining words
Html:
Copy Code code as follows:

<div>
<textarea id= "Txtprizenote" runat= "Server" height= "74px" width= "480px" maxlength= "10"
Style= "width:480px; height:74px; Float:left "></textarea>
<span style= "color:red;" >*</span><br/>
Remaining words: <span id= "showmsg" style= "color:red" ></span>
</div>

Copy Code code as follows:

<script type= "Text/javascript" >
Returns the byte length of Val
Function Getbytelen (val) {
var len = 0;
for (var i = 0; i < val.length; i++) {
if (Val[i].match (/[^\x00-\xff]/ig)!= null)//Full angle
Len + 2;
Else
Len + 1;
}
return Len;
}
Returns the value of Val within the specified byte length max
Function Getbyteval (val, max) {
var returnvalue = ';
var bytevallen = 0;
for (var i = 0; i < val.length; i++) {
if (Val[i].match (/[^\x00-\xff]/ig)!= null)
Bytevallen + 2;
Else
Bytevallen + 1;
if (Bytevallen > Max)
Break
ReturnValue + = Val[i];
}
Return returnvalue;
}
$ (function () {
var _area = $ (' textarea#txtprizenote ');
var _info = _area.next ();
var _max = _area.attr (' maxlength ');
var _val;
_area.bind (' KeyUp change ', function () {//bind KeyUp and change events
if (_info.find (' span '). Size () < 1) {//avoid inserting a message every time it bounces
_info.append (_max);
}
_val = $ (this). Val ();
_cur = Getbytelen (_val);
if (_cur = = 0) {//when the default value is 0 o'clock, the number of entries can be the default maxlength value
_info.text (_max);
else if (_cur < _max) {//when the default value is less than the limit, the number can be entered as Max-cur
_info.text (_max-_cur);
else {//when the default value is greater than or equal to the limit
_info.text (0);
$ (this). Val (Getbyteval (_val,_max)); Intercepts values within a specified byte length
}
});
});
</script>

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.