How to disable text when using placeholder?
<input type="number" placeholder="Min.">
EDIT
The input
number should allow the user to enter only numbers.
How to disable text when using placeholder?
<input type="number" placeholder="Min.">
EDIT
The input
number should allow the user to enter only numbers.
The jquery numeric plugin did the trick. Thank you all for the help. I had to write more text because the answer should be 30 characters minimum.
$(document).ready(function(){
$(".numeric").numeric();
});
This stops the user from inputting any character which is not a number, while still allowing them to use non-printable keys (ctrl, alt, backspace, enter, etc.)
This first part is from this answer to a different question
The second part basically checks every time the user presses a key, to see if that key was...
If both of these conditions are met, prevent the character from being entered.
// https://stackoverflow.com/a/12467610/4639281
function printable(keycode) {
var valid =
(keycode > 47 && keycode < 58) || // number keys
keycode == 32 || keycode == 13 || // spacebar & return key(s) (if you want to allow carriage returns)
(keycode > 64 && keycode < 91) || // letter keys
(keycode > 95 && keycode < 112) || // numpad keys
(keycode > 185 && keycode < 193) || // ;=,-./` (in order)
(keycode > 218 && keycode < 223); // [\]' (in order)
return !!valid;
}
// This part is me
document.getElementById('min').onkeydown = function(e) {
var char = String.fromCharCode(e.keyCode);
if(printable(e.keyCode) & isNaN(char)) {
e.preventDefault();
}
}
<input type="text" placeholder="Min." id="min">