[JS] code that limits the number of text bytes with JavaScript regular expressions

Time:2021-10-8

When doing the project, write a JS for everyone. The problem of limiting the number of words in text is often used in practical development; The main problem is the restriction on Chinese. The following code solves the verification problem of limiting the number of bytes; As long as the code is saved in a JS file and introduced into the verification page, it can be used! At the same time, I hope you will give me strong support and valuable opinions. I will publish more good articles in my spare time in the future. Thank you!!  

The following is a reference fragment:
/*  
  value:   Value;   
  Bytelength: database byte length   
  Title: Chinese name of the field   
  Attribute: attribute name   
  Instructions for use:   

add to   (1)   Onkeyup = “limitlength (this. Value, 100, ‘name’, ‘name’)”   
           (2)    id=”name”   Or [struts tag] styleid = “name”   
  Note: ID and name   The attribute name should be 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’)”  >  

*/  

Copy codeThe code is as follows:

function limitLength(value, byteLength, title, attribute) {   
       var newvalue = value.replace(/[^\x00-\xff]/g, “**”);   
       var length = newvalue.length;   

       // When the number of bytes filled in is less than the set number of bytes    
      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  *  one  –  1,   1);// Check point is“ ×”   

    // if   The base point is ×;  It is judged that there is within the base point × Even or odd     
     if (count % 2 == 0) {   
              // When even    
            size = count / 2 + (byteLength * 1 – count);   
            limitvalue = value.substr(0, size);   
    } else {   
            // When it is odd    
            size = (count – 1) / 2 + (byteLength * 1 – count);   
            limitvalue = value.substr(0, size);   
    }   
    alert(title  +  “Maximum input”  +  byteLength  +  “Bytes (equivalent to” + bytelength)  / 2 + “Chinese characters)!”);    
   document.getElementById(attribute).value = limitvalue;   
   return;   
}   

Recommended Today

New childhood fairy tale [139] little prince 32 — I planted this tree

Pain makes people sober. Everything in life originally depends on themselves. The mercy of others can’t win a better future. A petite thief said, “this time… I planted the tree and this time… I opened the road. If you want to buy the road money this time… ························································································· Another tall thief who blocked the way […]