0

I have an input tag of type text.

The user is supposed to enter a float or int, greater than 4. How to I verify that the user has not entered any non-numeric characters ? Decimal points are ok:

For example:

  1. 10.1 (ok)
  2. 10 (ok)
  3. 10$ (not ok)
  4. abcs (not ok)
  5. .0.12 (not ok -> Too many periods)
  6. 4 (not ok -> number must be greater than 4)
  7. 1a (not ok)

How do I do this? Parsing each and every character in the text box seems clumsy, and I'm don't know think I can do this with a regex. Any suggestions ?

Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190
  • You should take a look at jquery-inputmask: https://github.com/RobinHerbots/jquery.inputmask – Fals Jan 24 '14 at 19:15
  • almost what you want: http://stackoverflow.com/questions/17299330/javascript-regex-validating-a-double-float – caramba Jan 24 '14 at 19:15

2 Answers2

0

Check out isNaN() and carefully read through how to use it. Until ECMA 6 is widely used, isNaN() is probably the best in conjunction with typeof.

iamchris
  • 2,621
  • 1
  • 16
  • 15
0

Use parseFloat():

Here's a JSFiddle example: http://jsfiddle.net/HbqF3/3/

sundance
  • 2,905
  • 4
  • 21
  • 31