Posted By

melvitax on 09/05/08


Tagged

jquery validation


Versions (?)

Who likes this?

7 people have marked this snippet as a favorite

1man
classicalart
dine
johnfmorton
ts
felipefermin
gudeidea


Jquery Limit Input to Numbers


 / Published in: JavaScript
 

  1. $('input.valid-number').bind('keypress', function(e) {
  2. return ( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
  3. })

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: analogNerd on June 26, 2012

This code won't catch the alternate values on the numeric keys. For example, #, ! and @ will all be allowed as the key pressed for these is still the number key.

I changed the code to account for this as follows:

$('input.valid-number').bind('keypress', function(e) { return (e.which === 8 || e.which === 0 || (e.shiftKey === false && (e.which > 47 && e.which < 58))) ? true : false; }

Posted By: analogNerd on June 26, 2012

Forgive the lack of formatting, this was my first post and I'm not used to Markdown.

$('input.valid-number').bind('keypress', function(e) {   
    return (e.which === 8 || e.which === 0 || (e.shiftKey === false && (e.which > 47 && e.which < 58))) ? true : false;   
}

You need to login to post a comment.