Please help me to adjust an existing script to replace COMMA with DOT. I use a script which limit the inserting character into Text fields. Only 1,2,3,4,5,6,7,8,9,0 and "." and "," are accepted to be inserted. I would like to have two buttons of inserting DOT - key==188 (ma) and key== 190 (dot).
jQuery.fn.ForceNumericOnly =
function()
{
return this.each(function()
{
$(this).keydown(function(e)
{
var key = e.charCode || e.keyCode || 0;
return (
key == 8 ||
key == 9 ||
key == 46 ||
key == 110 ||
key == 188 ||
key == 190 ||
(key >= 35 && key <= 40) ||
(key >= 48 && key <= 57) ||
(key >= 96 && key <= 105));
});
});
};
$("#iMONEY").ForceNumericOnly();
It can be tested HERE
Please help me to adjust an existing script to replace COMMA with DOT. I use a script which limit the inserting character into Text fields. Only 1,2,3,4,5,6,7,8,9,0 and "." and "," are accepted to be inserted. I would like to have two buttons of inserting DOT - key==188 (ma) and key== 190 (dot).
jQuery.fn.ForceNumericOnly =
function()
{
return this.each(function()
{
$(this).keydown(function(e)
{
var key = e.charCode || e.keyCode || 0;
return (
key == 8 ||
key == 9 ||
key == 46 ||
key == 110 ||
key == 188 ||
key == 190 ||
(key >= 35 && key <= 40) ||
(key >= 48 && key <= 57) ||
(key >= 96 && key <= 105));
});
});
};
$("#iMONEY").ForceNumericOnly();
It can be tested HERE
Share Improve this question edited Apr 18, 2014 at 20:11 rene 42.5k78 gold badges121 silver badges165 bronze badges asked Oct 20, 2013 at 7:33 Sergiu CostasSergiu Costas 5604 gold badges9 silver badges26 bronze badges 5- It is not clear what problem you're having. – Pointy Commented Oct 20, 2013 at 7:36
- I do not know how to replace COMMA with DOT... – Sergiu Costas Commented Oct 20, 2013 at 7:37
- It is easy to give up from COMMA, but I would like to have ACTIVE button COMMA but inserting DOT – Sergiu Costas Commented Oct 20, 2013 at 7:38
- 1 I think it'd be easier to catch "keypress" and simply replace any mas in the field value with dots. – Pointy Commented Oct 20, 2013 at 7:40
- I honestly would avoid such approach, unless it's okay to you don't replace ma with dot if the user paste a text with ma in the text field (using keyboard or mouse). Same it's applying for drag & drop. – ZER0 Commented Oct 20, 2013 at 8:33
4 Answers
Reset to default 5Just use
if(e.keyCode == 188){
e.preventDefault();
$(this).val($(this).val() + '.');
}
Here you go. :)
For future references Mini-Tutorial.
The value of the textbox is updated after keypress event is fired. It's not a place to replace ma with dot. Use keyup event instead:
jQuery.fn.ForceNumericOnly =
function()
{
this.keyup(function(e)
{
// console.log("Change");
$(this).val($(this).val().replace(/,/g,"."));
});
};
$("#iMONEY").ForceNumericOnly();
DEMO
var key = e.charCode || e.keyCode || 0;
// 110 is numpad ma code
if (key === 188 && key === 110) {
e.preventDefault();
$(this).val($(this).val() + '.');
}
You need to use the Replace method
var someVariable = "1,2,3,4,5,6,7,8,9,0";
$mylabel.text( someVariable.replace(',', '.') );
EDIT: If you are checking from TextBox then do it like this:
if(Key == 188){
var someVariable = $("#TEXTBOXID").val();
somVariable = someVariable.replace(',', '.');
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743706070a4493387.html
评论列表(0条)