javascript - How to make dot and comma sensation in GridView Textbox? -
i wanna put dot , comma sensation in gridview textbox, example have textbox field in asp.net gridview money field. have input 1234 dollars , 50 cent. if write 1234 => keypress should return 1.234 after comma 1.234. pressing comma "," ) should stop "." intellisense 1.234,50 cent. not work properly.
if start write
on keyup should start change it. should start change input : (while writing)
1234 dollars 50 centinput (123450) => 1.234,50
1234567 dollars 75 cent(1234567(you can put comma.it should not prevent)75) => 1.234.567,75)
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 = ""; (var = 0; < txtval.length; i++) { //alert(txtval.substring(i, 1)); newval = newval + txtval.substring(i, + 1); if ((i + 1) % 3 == 0 && != 0 && + 1 < txtval.length) { newval = newval + "."; } } txtobj.value = newval; } } function replaceall(txt, replace, with_this) { return txt.replace(new regexp(replace, 'g'), with_this); }
according requirement please have on example
$(document).ready(function () { $(".btnsubmit").click(function () { var txtobj = $("#txtinput").val(); if (txtobj.length != "") { var newval = ""; var arr = txtobj.split(' '); if (arr[0].length >= 4) { var doller = arr[0]; var cents = arr[2]; var decimal = "."; var position = doller.length - 3 var output = [doller.slice(0, position), decimal, doller.slice(position)].join('') + "," + cents; $("#txtinput").val(output); } } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <input type="text" id="txtinput" value="1234 dollers 50 cents" /><br /> <input type="submit" id="btnsubmit" class="btnsubmit" />
below code according comment
$(document).ready(function () { $(".btnsubmit").click(function () { var txtobj = $("#txtinput").val(); if (txtobj.length != "") { var newval = ""; var doller = txtobj; if (doller.length >= 4) { var decimal = "."; var comma = ","; var posdecimal = doller.length - (doller.length - 1); var poscomm = doller.length - 1; var output = [doller.slice(0, posdecimal), decimal, doller.slice(posdecimal)].join(''); var finaloutput = [output.slice(0, poscomm), comma, output.slice(poscomm)].join(''); $("#txtinput").val(finaloutput) } } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <input type="text" id="txtinput" value="123450" /><br /><br /> <input type="submit" id="btnsubmit" class="btnsubmit" />
Comments
Post a Comment