Limit the number of text bytes js Code _ Form Effects

Source: Internet
Author: User
Do the project when you write a section of JS to everyone. On the text limit the number of words, in the actual development often used; The main problem in the Chinese language, the following code to solve the problem of the limit of bytes of the checksum, as long as the next code saved to a JS file and introduced to the validated page, you can use! At the same time hope that we give strong support and valuable advice, I will be idle in the future, to publish more good articles, thank you!!


/*
Value: Values;
ByteLength: Database byte length
Title: Field Chinese name
Attribute: Property Name
Instructions for using:

Add (1) onkeyup= "Limitlength (this.value,100, ' name ', ' names ')"
(2) id= "name" or "Struts label" styleid= "name"
Note: The ID name and attribute property name are the same

Example: <textarea name= "Explain" id= "explain" onkeyup= "Limitlength" (value,5, ' semantic description ', ' explain ') ">

Or

<input type= "text" name= "explain" id= "explain" onkeyup= "Limitlength (value,5, ' semantic description ', ' explain ')" >

*/



function Limitlength (value, ByteLength, title, attribute) {
var newvalue = Value.replace (/[^\x00-\xff]/g, "* *");
var length = Newvalue.length;

When the number of bytes filled out is less than the number of bytes set
if (length * 1 <=bytelength * 1) {
Return
}
var limitdate = newvalue.substr (0, ByteLength);
var count = 0;
var limitvalue = "";
for (var i = 0; i < limitdate.length; i++) {
var flat = Limitdate.substr (i, 1);
if (flat = = "*") {
count++;
}
}
var size = 0;
var istar = newvalue.substr (ByteLength * 1-1, 1);//Check point is "x"

If base point is x; To determine if x is even or odd at the base point.
if (count% 2 = 0) {
When an even number is
Size = COUNT/2 + (bytelength * 1-count);
Limitvalue = value.substr (0, size);
} else {
When the number is odd
Size = (count-1)/2 + (ByteLength * 1-count);
Limitvalue = value.substr (0, size);
}
Alert (title + "Maximum Input" + ByteLength + "byte" (equivalent to "+bytelength/2+" characters)! ");
document.getElementById (attribute). value = Limitvalue;
Return
}

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.