Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

only allow numbers in text input in js

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

javascript only allow numbers

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

PREVIOUS NEXT
Code Example
Javascript :: column to comma separated string in mongodb 
Javascript :: node redirect 
Javascript :: How to validate an unicode email address in JavaScript 
Javascript :: auth provider react 
Javascript :: xslt remove node 
Javascript :: useWidthSize 
Javascript :: how to join kak in javascript 
Javascript :: string contains js 
Javascript :: reducer function redux 
Javascript :: delay sleep 
Javascript :: scrape html table javascript 
Javascript :: canvas draw rect dashed 
Javascript :: nextjs override page route 
Javascript :: how to give default value in jquery 
Javascript :: find element in array underscore js 
Javascript :: javascript countdown timer including days 
Javascript :: defaultdeep lodash 
Javascript :: jquery rename id 
Javascript :: cant see serviceWorker.js 
Javascript :: javascript target closest id 
Javascript :: omit object javascript 
Javascript :: separador de miles javascript 
Javascript :: nodemon 
Javascript :: grapesjs cdn 
Javascript :: hot get access_token instead of url 
Javascript :: how to give data from react native to webview 
Javascript :: params scope in javascript 
Javascript :: index of javascript 
Javascript :: html to jsx 
Javascript :: adding hbs partials in express.js 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =