.dnkl_thousandSeparator()

.dnkl_thousandSeparator()

This jQuery plugin separates a numeric value by a given character in an input field on blur.

jQuery.fn.dnkl_thousandSeparator = function(separatorSettings) {
    /*
    NAME:	dnkl_thousandSeparator([sep, dec])
    VERS:	1.0
    DESC:	Separates a numeric value from 1234567,89 into 1.234.567,89
    USAGE:	$('#myInputID').dnkl_thousandSeparator({ sep: 'mySeparatorCharacter', dec: 'myDecimalCharacter' });
    */
    var settings = $.extend({ sep: '.', dec: ',' }, separatorSettings || {});
    return $(this).each(function() {
        $(this).blur(function() {
            var a = $(this).val().split(settings.dec);
            var v = a[0].replace(/[^\-0-9]/g, '');
            var reg = /(-?\d+)(\d{3})/;
            while (reg.test(v)) { v = v.replace(reg, '$1' + settings.sep + '$2'); }
            if (a[1] != null) { v += settings.dec + a[1]; }
            $(this).val(v);
        });
        $(this).focus(function() {
            var a = $(this).val().split(settings.dec);
            var v = a[0].replace(/[^\-0-9]/g, '');
            if (a[1] != null) { v += settings.dec + a[1]; }
            $(this).val(v);
        });
    });
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>