# How to Validate a Decimal Number in JavaScript

In JavaScript, a number can be an integer, decimal but also hexadecimal. The number of validation methods provided will indicate that a number is true in all three cases.

If we want to verify that a number is a decimal, we must use other means of validation. One solution is to combine two different validation methods. The isNaN (is not a Number) method checks that a variable is not a number. It will return false if the variable is a decimal or hexadecimal integer. The function isFinite () checks that the variable in parameter is a finite number. It does not validate a hexadecimal number but will return true for an operation, For example, 5-3.

### Combining two function[edit]

Combining these two functions in a test, we get a way to verify that a number is decimal:

```
var test =! isNaN (parseFloat (3.5)) && isFinite (3.5) // test gets the true value
var test =! isNaN (parseFloat (ab4c)) && isFinite (ab4c) // test gets false
var test =! isNaN (parseFloat (hello)) && isFinite (hello) // test gets false
```

### Using regular expression[edit]

Another solution is to use a regular expression. This is written as ^ - {0,1} d *. {0,1} d + $ and decomposes as follows:

```
^: beginning of the chain
- {0,1}: the hyphen for a negative number absent or present only once
d *: from zero to several digits
. {0,1}: the point separating the integer part of the decimal part, absent or present only once
d +: at least one digit
$: end of the chain
```

**Example:**

```
var testDecimal = /^-{0,1}d*.{0,1}d+$/;
var test = testDecimal.test (5.3); // test gets the true value
var test = testDecimal.test (a4); // test gets the wrong value
var test = testDecimal.test (hello); // test gets the wrong value
```