Á¤±Ô½ÄÀ» »ç¿ëÇÏ¿© ¼ýÀÚ¸¸, ¿µ¹®¸¸, ¿µ¹®°ú ¼ýÀÚ¸¸ ±×¸®°í ¿ì¸® À§´ëÇÑ Çѱ۸¸ ÀÔ·ÂÇÒ ¼ö ÀÖ°Ô ÇÏ¿´´Ù.
Å×½ºÆ®´Â À©µµ¿ìÁî Å©·Ò, ¸Æ Å©·Ò, ¸Æ »çÆĸ®, ¿ìºÐÅõ ÆÄÆø¿¡¼ Çغ¸¾Ò°í ¸ðµÎ Á¤»ó ÀÛµ¿ Çϴ°ÍÀ» È®ÀÎÇÏ¿´´Ù. À̺¥Æ® keyCode¸¦ ¹Þ¾Æ ó¸®ÇÏ´Â Äڵ嵵 º¸¾ÒÁö¸¸ ÀÌ ¹æ¹ýÀÌ ÄÚµùµµ Àû°í °£´ÜÇÏ¿© ±¸ÇöÇغ¸¾Ò´Ù. Á¤±Ô½Ä Âü ¿ä±äÇÏ°Ô ¾²ÀδÙ. ³¯Àâ¾Æ¼ Á¤±Ô½ÄÀ» ´Ù½Ã Çѹø ºÁ¾ß°Ú´Ù.
¼öÁ¤»çÇ×
- ÀÔ·ÂµÈ keyCode°ªÀ» ¾Ë¾Æ³»´Â ºÎºÐ (*$("#input1").keyup(function(event){) À» keydown¿¡¼ keyupÀ¸·Î º¯°æ. keydownÀ¸·Î ÇßÀ» °æ¿ì ùÀÔ·Â ±ÛÀÚ°¡ ´©¶ôµÇ´Â Çö»óÀÌ ÀÖ¾î ¼öÁ¤
- ¼ýÀÚ¸¸, ¿µ¹®¸¸, ¿µ¹® ±×¸®°í ¼ýÀÚ¸¸ ÀÔ·Â ¹Þ´Â ºÎºÐ¿¡¼ Á¤±Ô½Ä¸¸ »ç¿ëÇÒ °æ¿ì ¹æÇâÅ°µµ ¸·¾Æ¹ö¸®´Â ¿À·ù°¡ ÀÖ¾î ÀÔ·ÂµÈ keycode°ªÀÌ
¹æÇâÅ°°¡ ¾Æ´Ò °æ¿ì¸¸ ó¸®Çϵµ·Ï ºÐ±âó¸®
- ±×¸®°í À§´ëÇÑ Çѱ۸¸ ÀÔ·Â ¹Þ´Â ºÎºÐ Ãß°¡. (Çѱ۸¸ ¹Þ´Â ºÎºÐÀº ´ç¿¬ ÀÔ·ÂµÈ ¹®ÀÚ¿¡¼ ¿µ¹®°ú ¼ýÀÚ°¡ ÀÖÀ¸¸é ÇØ´ç ºÎºÐÀ» °ø¹éÀ¸·Î ´ëüÇÏ¿© Çѱ۸¸ ÀԷµǴ ȿ°ú¸¦ ³½°ÍÀÌ´Ù. ¹°·Ð ´Ù¸¥ ºÎºÐµµ ¸¶Âú°¡Áö.)
<html>
<head>
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#input1").keyup(function(event){
if(event.keyCode !=8){
var result = "keycode="+ event.keyCode + " value="+ String.fromCharCode(event.keyCode);
var preHtml = $("#result").html();
$("#result").html(preHtml+ "<br />" +result);
}
if($(this).val() ==""){
$("#result").empty();
}
});
$("#onlyNumber").keyup(function(event){
if (!(event.keyCode >=37 && event.keyCode<=40)) {
var inputVal = $(this).val();
$(this).val(inputVal.replace(/[^0-9]/gi,''));
}
});
$("#onlyAlphabet").keyup(function(event){
if (!(event.keyCode >=37 && event.keyCode<=40)) {
var inputVal = $(this).val();
$(this).val(inputVal.replace(/[^a-z]/gi,''));
}
});
$("#notHangul").keyup(function(event){
if (!(event.keyCode >=37 && event.keyCode<=40)) {
var inputVal = $(this).val();
$(this).val(inputVal.replace(/[^a-z0-9]/gi,''));
}
});
$("#onlyHangul").keyup(function(event){
if (!(event.keyCode >=37 && event.keyCode<=40)) {
var inputVal = $(this).val();
$(this).val(inputVal.replace(/[a-z0-9]/gi,''));
}
});
});
</script>
</head>
<body>
¼ýÀÚ¸¸: <input type="text" id="onlyNumber" /> <br />
¿µ¹®¸¸: <input type="text" id="onlyAlphabet" /> <br />
¿µ¹®,¼ýÀÚ¸¸:<input type="text" id="notHangul" /><br />
Çѱ۸¸:<input type="text" id="onlyHangul" /><br />
keyCode: <input type="text" id="input1" />
<div id="result">
</div>
</body>
</html>
================================ 2013 /10/ 05 IE ¹ö±× ¼öÁ¤ ======================================
<html>
<head>
<title>
input handler test
</title>
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript">
/*
conditions
- jquery obj for input
- data type(AP: alphabet, N: number, HA: hangul, AN: alphanumeric)
- event type (click, keydown, keypress, keyup(*default) ...)
- handler binded event type (*optional)
- max length( *optional)
*/
function addInputHandler(conditions){
var $input = conditions.input;
var dataType = conditions.dataType;
var eventType = conditions.eventType;
if ((!$input) || (!dataType)) {
throw {error:"NotEnoughArguments", errorMsg:"required argument is missing " +((!$input)?" target input element":" dataType")}
return;
}
if ($input[0].tagName != "INPUT") {
throw {error:"IlregalTargetElement", errorMsg:"target element is not input"};
return;
}
if ((!eventType)) {
eventType = "keyup";
}
var handlerFunc = conditions.handler;
if ((!handlerFunc)) {
handlerFunc = function(event){
$("#divKeyCode").empty().html("<span> event key code = "+event.keyCode+"</span>");
var regEx = null;
if (dataType == "N") {
regEx = /[^0-9]/gi;
} else if (dataType == "AP") {
regEx = /[^a-z]/gi;
}else if (dataType == "AN") {
regEx = /[^a-z0-9]/gi;
}else if (dataType == "HA") {
regEx = /[a-z0-9]/gi;
}else{
throw {error:"IlregalDataType", errorMsg:"dataType("+dataType+") is incorrect"}
}
remainOnlyTargetValue(regEx, $input,event);
//return true;
}; // end of handlerFunc
} // end of if to check handlerFunc
$input.on(eventType,handlerFunc);
if (conditions.maxlength) {
$input.attr("maxlength",conditions.maxlength);
}
}
function remainOnlyTargetValue(regEx, $input,event) {
if ((!(event.keyCode >=34 && event.keyCode<=40)) && event.keyCode != 16) {
var inputVal = $input.val();
if (regEx.test(inputVal)) {
$input.val(inputVal.replace(regEx,''));
}
}
}
$(document).ready(function(){
try {
addInputHandler({input:$("#onlyNumber"),dataType:"N",maxlength:7});
addInputHandler({input:$("#onlyAlphabet"),dataType:"AP"});
addInputHandler({input:$("#alphaNumeric"),dataType:"AN"});
addInputHandler({input:$("#hangul"),dataType:"HA"});
} catch(e) {
console.log(e);
}
});
</script>
</head>
<body>
<label for="onlyNumber">onlyNumber:</label><input type="text" name="onlyNumber" id="onlyNumber" /> <br />
<label for="onlyAlphabet">onlyAlphabet:</label><input type="text" name="onlyAlphabet" id="onlyAlphabet" /> <br />
<label for="alphaNumeric">alphaNumeric:</label><input type="text" name="alphaNumeric" id="alphaNumeric" /> <br />
<label for="hangul">hangul:</label><input type="text" name="hangul" id="hangul" /> <br />
<select name="testSelect" id="testSelect">
<option value="1">1</option>
</select>
<div id="divKeyCode"></div>
</body>
</html>
µ¡±Û|µ¡±Û ¾²±â