-3

I have a number field in the form and am validating that number field for float values. But I need to restrict the float number to only one period (.)

For example: 122.00

But now its taking input like this also: 123.00.. I dont want allow another period (.) in the end again.

Is there any regex for this?

YakovL
  • 7,557
  • 12
  • 62
  • 102
  • 2
    Possible duplicate of [regular expression for floating point numbers](http://stackoverflow.com/questions/12643009/regular-expression-for-floating-point-numbers) – Mr. Llama Jun 19 '16 at 23:50
  • [Why use regular expressions?](http://stackoverflow.com/a/37913037/477563) JavaScript includes `parseFloat`. If the return value is NaN then the input is not a valid float. – Mr. Llama Jun 19 '16 at 23:56

1 Answers1

2

Why use regular expressions? JavaScript includes parseFloat which returns NaN if the input was not a valid floating point:

var f = parseFloat(someString);
if ( isNaN(f) ) {
    // someString is not a valid float
}
Mr. Llama
  • 20,202
  • 2
  • 62
  • 115