SeaSide SeaSide - 1 year ago 65
ASP.NET (C#) Question

Restrict jQuery event only on some text boxes

I have this jQuery which automatically adds a dot on thousand:

$(function () {

$(document).on("keyup", "input[type=text]", function () {

if (event.which >= 37 && event.which <= 40) return;

// format number
$(this).val(function (index, value) {
return value

//doadaje tacku na hiljade:
.replace(/\D/g, "")
.replace(/\B(?=(\d{3})+(?!\d))/g, "\.")


On my form I have one textbox for date and all others textboxes are for numerical input. I want to restrict this jQuery to works only with textboxes for numerical input. Now this jQuery formatting and textbox for date but I don't want to do that on that field.

Answer Source

Each textbox has his own ID name

I will rather suggest to use an identifier to distinguish between inputs which will accept only number. Then use this class to trigger event

$(function () {
   $(document).on("keyup", "input[class=onlyNum]", function () {
            //Rest of code