
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How can we validate decimal numbers in JavaScript?
Following is the code to validate decimal numbers in JavaScript −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 20px; font-weight: 500; } </style> </head> <body> <h1>Validate decimal numbers in JavaScript</h1> <div style="color: green;" class="result"></div> <input type="number" class="num" /> <button class="Btn">CHECK</button> <h3>Click the above button to check if a valid decimal number is entered or not</h3> <script> let resEle = document.querySelector(".result"); document.querySelector(".Btn").addEventListener("click", () => { var str = document.querySelector(".num").value; var regex = /^[-+]?[0-9]+\.[0-9]+$/; var match = str.match(regex); if (match) resEle.innerHTML = "The number given is a decimal number"; else resEle.innerHTML = "The number given is not a decimal number"; }); </script> </body> </html>
Output
The above code will produce the following output −
On entering a normal number and clicking on ‘CHECK’ button −
On entering a decimal number and clicking on ‘CHECK’ −
- Related Articles
- How to validate decimal numbers in JavaScript?
- Counting numbers after decimal point in JavaScript
- How can we generate Strong numbers in Python?
- How can we use complex numbers in Python?
- JavaScript algorithm for converting Roman numbers to decimal numbers
- How to validate URL address in JavaScript?
- How to validate email address in JavaScript?
- How can I validate EditText input in Android?
- How to validate a given number in JavaScript?
- How to validate a date pattern in JavaScript?
- How can we debug JavaScript in Internet Explorer?
- How can I remove the decimal part of JavaScript number?
- How can we make JTextField accept only numbers in Java?
- How can I round a number to 1 decimal place in JavaScript?
- How to Convert Hex Numbers to Decimal Numbers in Excel?

Advertisements