The isDecimal
function checks if the input string or number represents a valid decimal number. It returns true
if the value is a valid decimal number and follows one of the supported formats, and false
otherwise.
The function can be imported using ES6 syntax from the "multiform-validator" package:
import { isDecimal } from 'multiform-validator';
Alternatively, you can import the function using CommonJS syntax with require
(Node.js):
const { isDecimal } = require('multiform-validator');
The function takes one parameter:
value
(string|number) - The input value to check if it represents a valid decimal number.// Example 1 - Valid decimal numbers
const result1 = isDecimal('123.45');
console.log(result1); // true
const result2 = isDecimal('-123.45');
console.log(result2); // true
const result3 = isDecimal('0.123');
console.log(result3); // true
const result4 = isDecimal('1,234.56');
console.log(result4); // true
// Example 2 - Invalid decimal numbers
const result5 = isDecimal('1.234,56');
console.log(result5); // false (invalid format)
const result6 = isDecimal('abc');
console.log(result6); // false (not a valid number)
const result7 = isDecimal('12a.34');
console.log(result7); // false (not a valid number)
const result8 = isDecimal('12.34.56');
console.log(result8); // false (not a valid number)
The function expects the input value to be passed as a string or a number representing a decimal number. If the input is not a string or number, or an empty string, the function throws an error. It uses a regular expression to validate decimal numbers, supports both dot (.) and comma (,) as decimal separators, and checks for multiple decimal separators. Additionally, it performs checks for the negative sign to ensure it is only at the beginning of the value.