Home >Web Front-end >JS Tutorial >Specific examples of JS verification control input Chinese and English byte length (input, textarea, etc.)_javascript skills

Specific examples of JS verification control input Chinese and English byte length (input, textarea, etc.)_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:32:011079browse

Check whether the form meets the specified length. The maximum allowed is n characters (Chinese is counted as 2 characters)!

Copy code The code is as follows:

function fucCheckLength(strTemp) {
var i, sum;
sum=0;
for(i=0;i if ((strTemp.charCodeAt(i)>=0) && (strTemp.charCodeAt( i)<=255)) {
sum=sum 1;
}else {
sum=sum 2;
}
}
return sum;
}

Use:
Copy code The code is as follows:

m= fucCheckLength(strTemp);
if(m>10){
alert("Length is greater than 10 characters!");
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn