소수점을 포함한 숫자만 입력가능한 Input 세팅

 

        $("#sample-table-1 input").each(function(){
            $(this).keyup(function(){
                if( $(this).val() != null && $(this).val() != '' ) {
                    var tmps = $(this).val().replace(/[^\.|^0(0)+|^0-9\.]/g, '');
                    /* 소수점은 하나만 입력되도록*/
                    var arr = tmps.split(".");
                    if(arr.length > 2) {
                        tmps = arr[0] + '.' + arr[1];
                    }
                    $(this).val(tmps);
                }
            });
            $(this).focusout(function() {
                if( $(this).val() != null && $(this).val() != '' ) {
                    var tmps = $(this).val().replace(/[^\.|^0(0)+|^0-9\.]/g, '');

                    /* 소수점은 하나만 입력되도록*/
                    var arr = tmps.split(".");
                    if(arr.length > 2) {
                        tmps = arr[0] + '.' + arr[1];
                    }
                    $(this).val(tmps);
                }
            });
        });

 


반응형

+ Recent posts