Gets the length of the character in the input box for form validation, and when the input is in English, the character length is 1, and when the Chinese is entered, the character length is 2.
The number of characters is 4~16 bit,
:
One Chinese to two English.
On the Code
1Btn.addeventlistener (' Click ', ov,false);2 3 functionov () {4 varInputvalue =Text.value.trim ();5 //remove the first and last space6 if(Countlength (Inputvalue) < 4 | | countlength (inputvalue) > 16) {7 //determine if the length is between 4-168pro.innerhtml = ' name format error ';9Pro.classname = ' Pro Warn ';TenText.classname = ' warn '; One}Else{ Apro.innerhtml = ' name format correct '; -Pro.classname = ' Pro Cor '; -Text.classname = ' Cor '; the } - } - - functioncountlength (str) { + varInputlength = 0; - //give a variable to record the length + for(vari = 0; i < str.length; i++) { A varCountcode =str.charcodeat (i); at //returns the Unicode encoding of the character at the specified position - //determine if the ASCII code is ASCII, the first 128 characters of the Unicode code - if(countcode >= 0 && countcode <= 128) { -Inputlength + +; -}Else{ -Inputlength +=2; in //if it is an extension code, then one time +2 - } to } + returninputlength; -}
JavaScript form validation, when entering Chinese, the character length is 2