Javascript Restrict Text Field to Numbers and Decimals

Javascript restrict text field to numbers and decimals?

Here I've added some code in your jsfiddle reference to allow decimal number and prevent user to enter "." symbol twice. Here's the complete code

$(document).ready(function () {
//called when key is pressed in textbox
$("#quantity").keypress(function (e) {
var tempInput = $("#quantity").val()
if(tempInput.split(".").length >= 2 && e.which == 46)
return false; //To check whether user has entered dot symbol before
//if the letter is not digit then display error and don't type anything
if (e.which != 110 && e.which != 46 && e.which != 190 && e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
//display error message
$("#errmsg").html("Digits Only").show().fadeOut("slow");
return false;
}
});
});

If anything goes wrong, feel free to ask.

P.S : I assume that you use "." symbol to represent float number

How can I restrict input on html to numbers with decimals using javascript?

I managed to make it work by changing the type of input field from number to text and using this

function forceNumericDecimal(){
var $input = $(this);
$input.val($input.val().replace(',', '.'));
$input.val($input.val().replace(/[^\d\.]+/g,''));
$input.val($input.val().replace(/\./, "x"));
$input.val($input.val().replace(/\./g, ""));
$input.val($input.val().replace(/x/, "."));
}

$('body').on('propertychange input', '.numberdecimal', forceNumericDecimal);

The regex replacements will change commas for dots and will not allow more than one dot.

Restricting input to textbox: it only accept number and should not accept decimal value

first you need to create an input element which call a function

<input type = "text" id = "a" onkeyup="myFunction(event)"/>

now you define your input box which will call a function myFunction (which takes event) whenever you will press a key on your keyboard.

now we need to prevent all the other key except 0-9 and without (.) becuase you don't want any decimal point.

so we need to create a regex which can check the pattern and check if the typed value follows the pattern or not if it follow we will set the value in a variable if not then we will set input value to old correct value and call preventDefault.

<script>
let value = null;
function myFunction(event) {

// event.target.value = what ever you typed
// /^[0-9]+$/ = regex code to prevent other letter except 0123456789

if (event.target.value.match(/^[0-9]+$/)) {
value = event.target.value;
} else {
// this line will update your input box to correct value
document.getElementById("a").value = value;
event.preventDefault();
}
}
</script>

Javascript/Jquery to restrict input field to one decimal and numbers only

I probably posted this question too soon without spending enough time debugging. Just thought id share my solution.

    $('#val00').on('paste', function(){

if (this.value !="") {
this.value = ""
}else{
this.value = this.value
.replace(/[^\d.]/g, '') // numbers and decimals only
.replace(/(\..*)\./g, '$1') // decimal can't exist more than once
.replace(/(\.[\d]{2})./g, '$1'); // not more than 2 digits after decimal
console.log("ON INPUT "+mynum);
mynum++;
}
})

How to restrict from entering a decimal value in input type number?

Preventing user input can be done with JavaScript. I'd use the input event for catching values, as it's a unified interface, encompassing any input method you can think of keyup, paste, pointer events, touch events, etc...