0

How to disable text when using placeholder?

jsfiddle

<input type="number" placeholder="Min.">

EDIT

The input number should allow the user to enter only numbers.

Tushar
  • 85,780
  • 21
  • 159
  • 179
brunodd
  • 2,474
  • 10
  • 43
  • 66

2 Answers2

0

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();
});
brunodd
  • 2,474
  • 10
  • 43
  • 66
0

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...

  1. Printable, and
  2. Not a number

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">
Community
  • 1
  • 1