Html & Script

textarea 글자제한

컨텐츠 정보

본문

<style type="text/css">

.progress{
        width: 1px;
        height: 14px;
        color: white;
        font-size: 12px;
  overflow: hidden;
        background-color: navy;
        padding-left: 5px;
}

</style>

<script type="text/JavaScript">


/***********************************************
* Form Field Progress Bar- By Ron Jonk- http://www.euronet.nl/~jonkr/
* Modified by idsu.net
***********************************************/

function textCounter(field,counter,maxlimit,linecounter) {
        // text width//
        var fieldWidth =  parseInt(field.offsetWidth);
        var charcnt = field.value.length;       

        // trim the extra text
        if (charcnt > maxlimit) {
                field.value = field.value.substring(0, maxlimit);
        }

        else {
        // progress bar percentage
        var percentage = parseInt(100 - (( maxlimit - charcnt) * 100)/maxlimit) ;
        document.getElementById(counter).style.width =  parseInt((fieldWidth*percentage)/100)+"px";
        document.getElementById(counter).innerHTML="총: "+percentage+"%"
        // color correction on style from CCFFF -> CC0000
        setcolor(document.getElementById(counter),percentage,"background-color");
        }
}

function setcolor(obj,percentage,prop){
        obj.style[prop] = "rgb(80%,"+(100-percentage)+"%,"+(100-percentage)+"%)";
}

</script>
<form>

<textarea rows="5" cols="40" name="maxcharfield" id="maxcharfield"
onKeyDown="textCounter(this,'progressbar1',20)"
onKeyUp="textCounter(this,'progressbar1',20)"
onFocus="textCounter(this,'progressbar1',20)" ></textarea><br />

<div id="progressbar1" class="progress"></div>

<script>textCounter(document.getElementById("maxcharfield"),"progressbar1",20)</script>

</form>
 

관련자료

댓글 0
등록된 댓글이 없습니다.
Today's proverb
누구나 잘못할 수는 있지만 누구나 솔직할 수 있는 것은 아닙니다. 그러나 진실한 사람의 아름다움은 무엇과도 바꿀 수 없습니다. 솔직함은 겸손이고 두려움 없는 용기입니다. 잘못으로 부서진 것을 솔직함으로 건설한다면 어떤 폭풍에도 견뎌낼 수 있는 강인함이 있습니다. 가장 연약한 사람이 솔직할 수 있으며 가장 여유로운 사람이 자신의 모습을 볼 수 있고 자신을 아는 사람만이 자신을 드러낼 수 있습니다.