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...
document.querySelector('input').addEventListener('input', e => {
e.target.value = Math.round(e.target.value.replace(/\D/g,''))
});
<input>
Javascript restrict input once 2 decimal places have been reached
var validate = function(e) { var t = e.value; e.value = (t.indexOf(".") >= 0) ? (t.substr(0, t.indexOf(".")) + t.substr(t.indexOf("."), 3)) : t;}
<input type="text" id="resultText" oninput="validate(this)" />
Related Topics
How to Change Background Opacity When Bootstrap Modal Is Open
Pass Node Js Variable to Ejs Template
Avoid a Link to Reload the Page in JavaScript
How to Get the Contextpath from Javascript, the Right Way
How to Count Text Lines Inside an Dom Element Can I
Datepicker Onchange Event Not Firing in Jquery
Detect If String Contains Any Spaces
How to Get the Url Parameters Using Angularjs
How to Make an Empty String Array in Typescript
How to Convert Uint8 Array to Base64 Encoded String
Use Lodash to Find Objects in an Array Matches an Id (Complex, Drilldown)
How to Iterate Through Json Nested Objects With Ngfor
Reprompt for Permissions With Getusermedia() After Initial Denial
How to Redirect Url After Login Successful Via Ajax Call in PHP
Check If a Number Has Repeated Digits
Setting Cross-Domain Cookies in Safari
How to Remove Captcha Verification from Firebase Phone Auth Using JavaScript
Assigning Id to All Elements of a Page in Angular Automatically