program/javascript

글자입력제한

yoursyun 2015. 8. 12. 10:01

<!DOCTYPE html>

<html lang="ko" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>

    <script type="text/javascript">

        function checklength(obj) {

            var maxlength = parseInt(obj.getAttribute("maxlength"));

            if (obj.value.length > maxlength) {

                alert("제한");
                obj.value = obj.value.substring(0, maxlength);;
               
            }

        }

    </script>
</head>
<body>
    <textarea id="hi" maxlength="5" onkeyup="return checklength(this)"></textarea>
</body>
</html>

반응형