javascript - Allow only numbers or decimal point in form field -


i've limited input field numbers through js not sure how allow decimals...

function isnumberkey(evt)       {          var charcode = (evt.which) ? evt.which : event.keycode          if (charcode > 31 && (charcode < 48 || charcode > 57))             return false;           return true;       } 

thank in advance!

answer:

function isnumberkey(evt)  {  var charcode = (evt.which) ? evt.which : event.keycode  if (charcode > 31 && (charcode < 48 || charcode > 57) && charcode != 46)     return false;   return true;  } 

adding charcode 46 worked (keypress value). 190 , 110 did nothing.

thanks all!

codes depend on event you're listening to, easy way find want using javascript event keycode test page here test input, e.g. full stop (the . left of right shift) have

               onkeydown    onkeypress    onkeyup event.keycode        190            46        190 event.charcode         0            46          0 event.which          190            46        190 

and . on numeric pad is

               onkeydown    onkeypress    onkeyup event.keycode        110            46        110 event.charcode         0            46          0 event.which          110            46        110 

as can see, uniform check onkeypress charcode it's unicode number; string.fromcharcode(46); // ".".

there full list on the mdn page keyboardevent stated

note: web developers shouldn't use keycode attribute of printable keys in keydown , keyup event handlers. described above, keycode not usable checking character inputted when shift key or altgr key pressed. when web developers implement shortcut key handler, keypress event better event purpose on gecko @ least. see gecko keypress event detail.

you can observe strange effects of using altgr or shift on keycode key of choice in test page linked well.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -