雁过请留痕...
代码改变世界

js 控制输入文字的字数

2017-06-02 14:57  xiashengwang  阅读(715)  评论(0编辑  收藏  举报

直接上代码。

<html>
<head>

</head>
<body>
<textarea id='txtArea' cols='50' rows='10'></textarea>
</body>
<script>

function getSelectionText() {
    var selText = "";
    if (window.getSelection) {  // 只有IE6,IE7,IE8不支持
        var sel = document.activeElement;
        if (sel &&
            (sel.tagName.toLowerCase() == "textarea" ||
             (sel.tagName.toLowerCase() == "input" &&
              sel.getAttribute("type").toLowerCase() == "text")))
        {
            var text = sel.value;
            selText = text.substring(
                sel.selectionStart,
                sel.selectionEnd
            );
        }
        else {
            var selRange = window.getSelection();
            selText = selRange.toString();
        }
    } else {
        if (document.getSelection) {  // 只有IE6,IE7,IE8,Firefox不支持. 代?并不会?行到?个分支
            range = document.getSelection();
            selText = range.toString();
        } else if (document.selection.createRange) { // 只有IE6~10支持
            var range = document.selection.createRange();
            selText = range.text;
        }
    }
    return selText;
}

function SetTextAreaMaxLength(controlId, length) {
    // JScript File for TextArea 
    // Keep user from entering more than maxLength characters 
    function doKeypress(control, length) {
        maxLength = length;
        value = control.value;
        if (maxLength && value.length > maxLength - 1) {
            event.returnValue = false;
            maxLength = parseInt(maxLength);
        }
    }
    // Cancel default behavior 
    function doBeforePaste(control, length) {
        maxLength = length;
        if (maxLength) {
            event.returnValue = false;
        }
    }
    // Cancel default behavior and create a new paste routine 粘贴这一块,只有IE有效,其他浏览器不支持window.clipboardData
    function doPaste(control, length) {

maxLength = length;
value = control.value;
if(maxLength){
event.returnValue = false;
maxLength = parseInt(maxLength);
var oTR = control.document.selection.createRange();
var iInsertLength = maxLength - value.length + oTR.text.length;
var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);
oTR.text = sData;

    }

function doDragenter(control, length) {
        maxLength = length;
        value = control.value;
        if (maxLength) {
            event.returnValue = false;
        }
    }
    function addEvent(elm, evType, fn, useCapture) {
        if (elm.addEventListener) {
            elm.addEventListener(evType, fn, useCapture);
            return true;
        } else if (elm.attachEvent) {
            var r = elm.attachEvent('on' + evType, fn);
            return r;
        } else {
            elm['on' + evType] = fn;
        }
    }
    function AttacheventTextAreaBeforePaste(obj, length) {
        return function() {
            doBeforePaste(obj, length)
        }
    }
    function AttacheventTextAreaPaste(obj, length) {
        return function() {
            doPaste(obj, length)
        }
    }
    function AttacheventTextAreaKeyPress(obj, length) {
        return function() {
            doKeypress(obj, length)
        }
    }
    function AttacheventTextAreaDragEnter(obj, length) {
        return function() {
            doDragenter(obj, length);
        }
    }
    var obj = document.getElementById(controlId);
    addEvent(obj, 'keypress', AttacheventTextAreaKeyPress(obj, length), null);
    addEvent(obj, 'beforepaste', AttacheventTextAreaBeforePaste(obj, length), null);
    addEvent(obj, 'paste', AttacheventTextAreaPaste(obj, length), null);
    addEvent(obj, 'dragenter', AttacheventTextAreaDragEnter(obj, length), null);
}

SetTextAreaMaxLength('txtArea',10);
</script>
</html>