JS determine the length of the input string (Chinese occupies two bytes, one byte English)

function  getByteLen (Val) {// pass a string
            
var  len  = 0 ; for  ( var  I  = 0 ; I  <  val.length; I ++ ) { IF  (Val [I] .match ( / [^ \ xOO - \ xFF] / IG)  ! = null // a full-                      len  + = 2 ; // If full-width, if mysql occupies two bytes is a field in the text, if the set encoded as utf-8, it is a Chinese 3 bytes, two bytes GBK the else                     len  + = . 1 ; // half-size one byte             } return  len;  
            
 
                
 
 
                

 

            

         }       

 

//transfer  

 if (getByteLen("追忆") < 3) { 

alert ( "length of not less than three characters"); 

}else{

alert ( "Enter the correct"); 

Published 90 original articles · won praise 21 · views 470 000 +

Guess you like

Origin blog.csdn.net/yx13649017813/article/details/42775807