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, I hope you will give us strong support and valuable advice I will be idle in the future, to publish more good articles, thank you!!
The following is a reference fragment:
/*
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
}