jquery number of characters (number letter length is 1, Chinese record is 2, over length auto-intercept)

Source: Internet
Author: User

<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-width
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 the bounce occurs
_info.append (_max);
}
_val= $ (this). Val ();
_cur = Getbytelen (_val);
if (_cur = = 0) {//when the default value length is 0 o'clock, you can enter the number as the default maxlength value
_info.text (_max);
} else if (_cur < _max) {//when the default value is less than the limit number, you can enter a number of max-cur
_info.text (_max-_cur);
} else {//when the default value is greater than or equal to the limit number
_info.text (0);
$ (this). Val (Getbyteval (_val,_max)); Intercept values within a specified byte length
}
});
});
</script>

jquery number of characters (number letter length is 1, Chinese record is 2, over length auto-intercept)

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.