Javascript implementation of the real string remaining word prompt instance code

  • 2020-03-26 21:33:43
  • OfStack


//Text box remaining word prompt (character size)
function textLimitCheckSj(thisArea, maxLength, SpanId) { 
    var str = thisArea.value; 
    if (getChrLen(str, maxLength) > maxLength * 2) { 
        thisArea.value = str.substring(0, x-1); 
    } 
    else { 
        var varss = '( The remaining words :' + Math.floor((maxLength * 2 - getChrLen(str, maxLength)) / 2) + ')'; 
        document.getElementById(SpanId).innerHTML = varss; 
    } 
} 
function getChrLen(str, maxLength) { 
    var realLength = 0, len = str.length, charCode = -1; 
    x = 0; 
    for (; (x < len) && (realLength <=maxLength * 2); x++) { 
        charCode = str.charCodeAt(x); 
        if (charCode >= 0 && charCode <= 128) 
            realLength += 1; 
        else
            realLength += 2; 
    } 
    return realLength; 
} 
?
<asp:TextBox ID="txtOwner_Name" runat="server" Width="200px" BackColor="LightYellow"
                        onkeyup="textLimitCheckSj(this,50,'spOwner_Name')"></asp:TextBox><span id="spOwner_Name"
                            style="color: #808080"><em> ( 50 Characters) </em></span> 


Related articles: