2

I used this number format jQuery

for displaying numbers with commas while typing. But when i typed letters also in the box, it's also taking as input. I need it to accept only numbers in and not letters, what to do?

Davis
  • 310
  • 1
  • 3
  • 14
  • 1
    Please add the relevant parts of your code... – nfechner Feb 28 '13 at 09:03
  • If i need to display Investment Amount in format(with commas after every 3 digits and two decimal points while typing the walue) i use the below code in my jsp file after including jQuery where "investmentAmount" is the id of Investment Amount. **$('#investmentAmount').number( true, 2 );** – Davis Feb 28 '13 at 09:17

3 Answers3

1

I suppose this could be considered dirty, but instead of having to update the plugin yourself, just filter the keys first:

$('#bla').on('keypress', function(evt) { var code = evt.which || evt.keyCode;

if (code < 57 || code > 127) {
  return;
}

evt.preventDefault();

}).number(true, 2);

Demo

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • i'm getting an error **Syntax error on token "_$tag_________________$tag________________", invalid AssignmentOperator** on second if statement. – Davis Feb 28 '13 at 09:28
  • @user2071789 It works fine in the `jsbin` example I've added at the bottom of my answer. – Ja͢ck Feb 28 '13 at 09:35
0

function isNumeric(n) { return !isNaN(parseFloat(n)) && isFinite(n); }

use this function to check each input

Clinton Ward
  • 2,441
  • 1
  • 22
  • 26
0

refer this post to restrict text box to numeric only

Numeric Validation

Or else you can write your own regular expression on keyup event and validate the input.

Happy coding

Community
  • 1
  • 1
K D
  • 5,889
  • 1
  • 23
  • 35