Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

html javascript input numbers only

<input type="text" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(..*?)..*/g, '$1');" />
Comment

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 :: node get root directory 
Javascript :: js check window active 
Javascript :: bash commands in node 
Javascript :: properly import mat icon angular 10 
Javascript :: vscode regex replace only group 
Javascript :: angular goto top of page 
Javascript :: js transition 
Javascript :: jquery check if type is checkbox 
Javascript :: store data in array jquery 
Javascript :: jquery get all checkbox checked 
Javascript :: js focus textarea 
Javascript :: discord.js rich embed 
Javascript :: react native submit on enter key 
Javascript :: random string js 
Javascript :: check if string matches regex js 
Javascript :: npm redux toolkit 
Javascript :: javascript remove trailing slash 
Javascript :: how to get json data from json file in node js 
Javascript :: how to find out which version of react 
Javascript :: merge two objects javascript 
Javascript :: javascript check if number is multiple of 3 
Javascript :: get cursor position in contenteditable div 
Javascript :: sequelize include only 
Javascript :: maximum sum subarray javascript 
Javascript :: javascript merge two objects 
Javascript :: find array with children javascript 
Javascript :: window is not defined Next Js 
Javascript :: statusbar reactnati 
Javascript :: async arrow function 
Javascript :: loopback upsert 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =