Home > Web Front-end > JS Tutorial > js implements character limit Chinese characters = two characters

js implements character limit Chinese characters = two characters

php中世界最好的语言
Release: 2018-04-17 17:12:02
Original
2863 people have browsed it

This time I will bring you js to implement character limit Chinese characters = two characters, js to implement character limit Chinese characters = two characters What are the precautions , the following is a practical case, let’s take a look one time.

html

<input type="text" id="txt">
Copy after login

Core js code

//字符串截取
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;
}
$('#txt').bind('keyup',function(){
	var val=this.value;
	if(val.replace(/[^\x00-\xff]/g,"**").length>14){
		this.value=getByteVal(val,14)
	}
})
Copy after login

Note: The code uses jquery bindingevent, so jqueryframework needs to be added.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Using js to implement car dashboard

##canvas and JS to implement dynamic clock animation

How to set up InstantClick to be compatible with MathJax and Baidu Statistics, etc.

The above is the detailed content of js implements character limit Chinese characters = two characters. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template