What is the best way to limit the amount of text that can be entered into a 'textarea'?

前端 未结 6 581
生来不讨喜
生来不讨喜 2021-01-19 05:50

What is the best way to limit the amount of text that a user can enter into a \'textarea\' field on a web page? The application in question is ASP .NET, but a platform agnos

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-19 06:27

    I use this, where the limit is a must. It also provides the user with the number of characters left.

    function CountLength(vControl)
        {
            var strValue = vControl.value;
            var vMax = 480;
            var vLeft = vMax - strValue.length;
            if (vLeft < 0)
            {
                vLeft = 0;
            }
            var vMessage = returnObjById('TextCounter');
            if (document.all)
            {
                vMessage.innerText = vLeft + ' characters remaining.';
            }
            else
            {   
                vMessage.textContent = vLeft + ' characters remaining.';
            }
            if (vLeft == 0)
            {
                vControl.value = vControl.value.substring(0, vMax);
            }
        }
    

提交回复
热议问题