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
우리가 행복한지의 여부는 우리들 각자가 진짜로 원하는 삶을 향해서 얼마나 당당하게 다가가고 있느냐에 달렸다. 그 채점 또한 저마다의 몫이다. (김한길)