1

I need to find whether a number inputed by a user is valid
Following cases are valid

 1. `12`  

 2. `12.01`

The following case is invalid:

1. `12.`  // decimal point with no number following

I have written a regular expression like

var decimalValidation = /^[0-9]*(\.?)[0-9]*$/;  
var n = decimalValidation .test(value);  

But the problem is that it accepts a value like 12.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
Robert_Junior
  • 1,122
  • 1
  • 18
  • 40

2 Answers2

4

Change your regex like below to match both integer and floating point numbers.

var decimalValidation = /^\d+(?:\.\d+)?$/;  

DEMO

Mulan
  • 129,518
  • 31
  • 228
  • 259
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
1

If this was the case, I would use the following as pattern:

var pattern = /^[0-9]+(\.[0-9]+)?$/

I hope this fits in your condition.