javascript - jQuery - Cannot retrieve Input Number value -


this question has answer here:

<input id='nb' name="nb" type="number" placeholder="number"/> 

jquery.val() returns value if there numbers, empty if there letter.

chrome shows input number arrows (and work), can type letters too.. weird thing

btw on mozilla , ie8 becomes normal input text (guess have old mozilla)

any idea ? couldn't find on jquery doc specific number inputs .val()

suggestion input type="text" instead html5 input input type="number":

//html  <input id="inp" type="text" />  //javascript (jquery needed)  function onlynumbers(evt) {     // options enter, backspace, home, end, arrows, etc.     var arrayexceptions = [8, 13, 16, 17, 18, 20, 27, 35, 36, 37,         38, 39, 40, 45, 46, 144];     if ((evt.keycode < 48 || evt.keycode > 57) &&             (evt.keycode < 96 || evt.keycode > 106) && // numpad             $.inarray(evt.keycode, arrayexceptions) === -1) {         return false;     } }  $('#inp').on('keydown', onlynumbers);  //javascript (without jquery)  function inarray(value, arr) {     for(var = 0; < arr.length; i++) {         if (value === arr[i]) {             return i;         }     }     return -1; }  function onlynumbers(evt) {     // options enter, backspace, home, end, arrows, etc.     var arrayexceptions = [8, 13, 16, 17, 18, 20, 27, 35, 36, 37,         38, 39, 40, 45, 46, 144];     if ((evt.keycode < 48 || evt.keycode > 57) &&             (evt.keycode < 96 || evt.keycode > 106) && // numpad               inarray(evt.keycode, arrayexceptions) === -1) {         return false;     } }  document.getelementbyid('inp').onkeydown = onlynumbers; 

as said in comment, until html5 becomes standard web language can work properly, prefer use this.

demo jquery
demo without jquery


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -