У меня есть плагин, который форматирует числа как валюту, вставляя десятичные и тысячи разделители:
1000 -> 1.000
10000,5 -> 10.000,5
, и я называю это так:
$('#input').myPlugin()
Но я хочу позвонитьэто путем указания символов, которые будут использоваться для десятичного и тысячного разделителей:
$('#input').myPlugin({thousand_type: ',' , decimal_type '.'}).
Как мне добиться этого в моем плагине?
Вот плагин:
(function( $ ){
$.fn.myPlugin = function() {
$('input').keypress(function(event){
if ((event.which < 48 || event.which > 57) && event.which != 8 && event.which != 44)
event.preventDefault();
});
$('input').keyup(function(event) {
event.preventDefault();
val = $(this).val();
if(val != ''){
thousand_type = '.';
if(thousand_type == '.'){
decimal_type = ',';
}else{
decimal_type = '.';
}
//remove thousand mark
if(thousand_type == '.'){
val = String(val).replace(/\./g, "");
}else{
val = String(val).replace(/\,/g, "");
}
//get position of decimal mark
pos_decimal = String(val).indexOf(decimal_type);
//device the number to thousand and decimal
if(pos_decimal > -1){
sub_decimal = String(val).substring(pos_decimal, String(val).length);
sub_thousand = String(val).substring(0, pos_decimal);
}else{
sub_decimal = '';
sub_thousand = val;
}
//1.111.111,33
//remove decimal mark
if(decimal_type == '.'){
removed_mark_val = String(val).replace(/\./g, "");
}else{
removed_mark_val = String(val).replace(/\,/g, "");
}
//check is Numeric
result = IsNumeric(removed_mark_val);
if(result == true){
if(thousand_type == '.'){
sub_thousand = String(sub_thousand).split("").reverse().join("")
.replace(/(.{3}\B)/g, "$1" + thousand_type)
.split("").reverse().join("");
}else{
sub_thousand = String(sub_thousand).split("").reverse().join("")
.replace(/(.{3}\B)/g, "$1" + thousand_type)
.split("").reverse().join("");
}
val = sub_thousand + sub_decimal;
//1111111,33
$(this).attr('value',val);
}else{
}
}
});
function IsNumeric(input){
var RE = /^-{0,1}\d*\.{0,1}\d+$/;
return (RE.test(input));
}
};
})( jQuery );