How to Validate Decimal Numbers in JavaScript ? Last Updated : 04 Mar, 2024 Comments Improve Suggest changes Like Article Like Report Validating user input is an essential aspect of Web Development. As a developer, when we are playing with the numeric inputs provided by the end-user, it is quite important to ensure that the input provided by the user is in the correct format. We can use the regular expression to Validate Decimal Numbers in JavaScript. The regular expression for same is given below. Regex for decimal in JavaScript:const decimalNumber = /^-?\d*\.?\d+$/; Below are the approaches used to Validate Decimal Numbers in JavaScript: Table of Content Using the match() methodUsing Regular ExpressionValidate Decimal Numbers using the match() methodThe match() method is a built-in JavaScript method that allows you to match a string against a regular expression. Example: Below is the example using the match() method. JavaScript function validateDecimalNumberUsingMatch(input) { // Use the match() method with a regular expression const isDecimal = input.match(/^-?\d*\.?\d+$/); // Return true if it's a valid decimal number, otherwise return false return isDecimal !== null; } // Example usage: const userInput = "3.14"; if (validateDecimalNumberUsingMatch(userInput)) { console.log("Approach 1: Valid decimal number!"); } else { console.log("Approach 1: Not a valid decimal number."); } OutputApproach 1: Valid decimal number!Validate Decimal Numbers using test()You can directly use a regular expression to test if the input string is a valid decimal number. Example: Below is the example using the test() method. JavaScript function validateDecimalNumberUsingRegExp(input) { // Define a regular expression for decimal numbers // This pattern allows for an optional negative sign, followed by digits, an optional decimal point, and more digits const decimalPattern = /^-?\d*\.?\d+$/; // Test if the input matches the pattern return decimalPattern.test(input); } // Example usage: const userInput = "3.14"; if (validateDecimalNumberUsingRegExp(userInput)) { console.log("Approach 2: Valid decimal number!"); } else { console.log("Approach 2: Not a valid decimal number."); } OutputApproach 2: Valid decimal number! Comment More infoAdvertise with us Next Article How to Validate Decimal Numbers in JavaScript ? G gauravggeeksforgeeks Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Validate Number String in JavaScript ? Validating a number string in JavaScript involves ensuring that a given string represents a valid number. This typically includes checking for digits, optional signs (+/-), decimal points, and possibly exponent notation (e.g., "1.23e4"). We will use various methods to validate number strings in Java 2 min read How to Format a Number with Two Decimals in JavaScript? In javascript, formatting numbers to a fixed number of decimal places is a common requirement, particularly in financial calculations, statistical data presentations, and to show in user interfaces. In this article we will explore different approaches to format a number with two decimals in JavaScri 2 min read How to Validate String Date Format in JavaScript ? Validating string date format in JavaScript involves verifying if a given string conforms to a specific date format, such as YYYY-MM-DD or MM/DD/YYYY. This ensures the string represents a valid date before further processing or manipulation. There are many ways by which we can validate whether the D 5 min read How to Remove the Decimal Part of a Number in JavaScript ? The decimal portion of a number in JavaScript can be removed through various methods, including the Math. trunc( ) method, Math. floor( ) method, Math. ceil( ) method, and Math. round( ) method. SyntaxMath.trunc(11.5) // Output: 11Math.floor(12.5) // Output: 12Math.ceil(15.5) // Output: 15Math.round 2 min read Number Validation in JavaScript Here are the various methods to validate numbers in JavaScript1. Check if a Value is a NumberUse typeof and isNaN() to determine if a value is a valid number.JavaScriptconst isNum = n => typeof n === 'number' && !isNaN(n); console.log(isNum(42)); console.log(isNum('42')); console.log(isNu 3 min read Like