JavaScript Number Validation (Decimal)

Decimals can be validated in JavaScript using a regular expression:

document.write(/^-?\d+(\.\d+)?$/.test('15.22'));

Broken down into sections:

^-?\d+ - The string being tested must begin with an optional minus sign, followed by 1 or more digits
(\.\d+)? - Followed by an optional period, which, if present must be followed by 1 or more digits.

Related Articles:
JavaScript Regular Expressions
JavaScript Regular Expression Special Characters: $ ^
JavaScript Number Validation (Integer)
JavaScript Regular Expression Plus Sign

3 Responses to “JavaScript Number Validation (Decimal)”

  1. kishore Says:

    Good one
    thanks a Lot

  2. Finaly some simplicity…thanks a lot…

  3. Helpful.
    Thanks

Leave a Reply