I have created a dynamic html table with some input controls and here I want to enter only decimal values with two digits after dot(.)
ex: 1111.22 , 44445454.33
Html code:
<div id="divdynamic"></div>
And I append dynamic data to this id divdynamic
jQuery code:
$(document).ready(function () {
decimaldata();
});
function decimaldata()
{
var data = `<div class="closedata"><input type="text" class="fields" name="txtdecimal" autocomplete="off" onkeyup="decimalonly('txt1',this)" value=""></div>
<div class="closedata"><input type="text" class="fields" name="txtdecimal1" autocomplete="off" onkeyup="decimalonly('txt2',this)" value=""></div>`
$("#divdynamic").append(data);
}
function decimalonly(n,t)
{
if(n == "txt1")
{
if (((event.which != 46 || (event.which == 46 && $(this).val() == '')) ||
$(this).val().indexOf('.') != -1) && (event.which != 45 || $(this).val().indexOf('-') != -1) &&
(event.which < 48 || event.which > 57)) {
event.preventDefault();
//alert('hit');
}
// here my logic
}
else
{
if (((event.which != 46 || (event.which == 46 && $(this).val() == '')) ||
$(this).val().indexOf('.') != -1) && (event.which != 45 || $(this).val().indexOf('-') != -1) &&
(event.which < 48 || event.which > 57)) {
event.preventDefault();
//alert('hit');
}
// here my logic
}
}
In the above method decimalonly(n,t)
I'm checking the entered key but that code is not working fine but if I entered any (non digit value) the alert('hit');
message is hitting but the entered non digit value is showing on the UI, it should not happen should only take decimals.
please suggest me how to achieve this.
Sorry for my bad English.