How can I put dot after 3 digits in asp.net textbox? Below code doesn't work for me. I want it in JavaScript not jQuery.
For example:
1234: 1.234
23456: 23.456
But if I put "COMMA", it should stop putting "." 1.2345,78
I guess comma has an event stopping dot event.
Such as first answer here Can jQuery add commas while user typing numbers?, instead of comma I want to use "dot" not comma. But I want to need permission for comma.
My C# code:
private void GvRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtVeri = (TextBox)e.Row.FindControl("txtVeri");
txtVeri.Attributes.Add("onkeyup", "InsertComma(this.id)");
}
}
My JS code:
function InsertComma(veriId) {
console.log("çalışıyor");
var txtObj = document.getElementById(veriId);
var txtVal = replaceAll(txtObj.value, '.', '');
//alert(txtObj.value);
if (txtObj.value != "") {
var newVal = "";
for (var i = 0; i < txtVal.length; i++) {
//alert(txtVal.substring(i, 1));
newVal = newVal + txtVal.substring(i, i + 1);
if ((i + 1) % 3 == 0 && i != 0 && i + 1 < txtVal.length) {
newVal = newVal + ".";
}
}
txtObj.value = newVal;
}
}
function replaceAll(txt, replace, with_this) {
return txt.replace(new RegExp(replace, 'g'), with_this);
}