“JavaScript hanya mengizinkan angka” Kode Jawaban

hanya mengizinkan angka dalam input teks di JS

$(document).ready(function() {
  $("#myTextBox").inputFilter(function(value) {
    return /^\d*$/.test(value);    // Allow digits only, using a RegExp
  },"Only digits allowed");
});
mandeep kumar

JavaScript hanya mengizinkan angka

function onlyNumbers(e) {
    // Check if the user pressed a number
    if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
        // If the user pressed a number, prevent the default action
        e.preventDefault();
    }

    // Then, add the event listener to the input
    document.getElementById("input").addEventListener("keypress", onlyNumbers);

        // If the key pressed is not a number, prevent the default action
        if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
            e.preventDefault();
        }

        // If the key pressed is a number, allow the default action
        if (e.which == 8 || e.which == 0 || (e.which >= 48 && e.which <= 57)) {
            return;
        }
    }

// Call the function
onlyNumbers(e);
Old-fashioned Otter

Jawaban yang mirip dengan “JavaScript hanya mengizinkan angka”

Pertanyaan yang mirip dengan “JavaScript hanya mengizinkan angka”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya