[event] Difference between 'onkeydown' and 'onkeypress'

Object
  • To notice what differences are between 'onkeydown' and 'onkeypress'
  • To make a page that is only mgiven Korean character.

List
  onkeydownonkeypress
 Digit Keyboard Key codeASCII Code
 Alphabet Keyboard Key code ASCII Code
 Korean Keyboard Key code(229)No Event
Function Key
(Del,Ctrl,Alt)
 Keyboard Key code No Event


Sample
<html>
<head>
<script type="text/javascript">
        function isHangul(event)
        {
var key;
            var keychar;
            
            if (window.event) {
                key = event.keyCode;
            } else if (event) {
                key = event.which;
            }
            
            var numcheck = /\d/;
    var alphcheck = /[a-zA-Z]/;
            var specialcheck = /[\s\t!@#$%^&amp;*()-_+=|\~`,.\/&lt;&gt;]/;

            keychar = String.fromCharCode(key);

alert("keychar" + keychar + "key : " + key);
if ( numcheck.test(keychar) ) {
                alert("숫자입니다,이름은 한글로 입력해주세요.");
                return false;
            }  else if ( alphcheck.test(keychar) ) {
                alert("영문,이름은 한글로 입력해주세요.");
                return false;
             }  else if ( specialcheck.test(keychar) ) {
                alert("한글 외에 기타 기호 또는 부호는 입력할 수 없습니다.");
 event.returnValue = false; // for IE8
                return false; // It won't work in IE8
            }  else {
return true;
    }
        }
</script>
</head>
<body>
keydown:<input type="text" onkeydown="return isHangul(event)" /><br/>
keypress:<input type="text" onkeypress="return isHangul(event)" />
</body>
</html>

Related Links

댓글

가장 많이 본 글