프로그래밍/JavaScript [JQuery] textbox focus on off일때 숫자 콤마 보여주기 도깨비방망이 2012. 8. 23. 17:53 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" language="javascript"> // Numeric only control handler jQuery.fn.ForceNumericOnly = function() { return this.each(function() { $(this).keydown(function(e) { var key = e.charCode || e.keyCode || 0; // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY return ( key == 8 || key == 9 || key == 46 || (key >= 37 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105) ); }); // 포커스 나갈때 콤마처리 $(this).focusout(function() { $(this).val($(this).val().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")); }); // 포커스 들어올때 콤마처리 $(this).focusin(function() { $(this).val($(this).val().replace(/\,/g, '')); }); // 키 눌렀을때 콤마처리 $(this).keyup(function() { $(this).val($(this).val().replace(/,/g, '')); $(this).val($(this).val().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")); }); }) }; window.onload=function(){ $('.num').ForceNumericOnly(); } </script> </HEAD> <BODY> <input type="text" class="num" name="number_1"/> <input type="text" class="num" name="number_2"/> <input type="text" class="num" name="number_3"/> </BODY> </HTML> 저작자표시