0

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);
    }

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Penguen
  • 16,836
  • 42
  • 130
  • 205

1 Answers1

-1

try building regex from http://regexr.com/ if you want to validate at a click event or any other such event. And if you want to restrict users input you need to tap on keypress, keyup or keydown and get the eventcode to get what exactly was pressed and return false accordingly. It would be better if you create a keypress event.