JSFiddle - React, Tailwind, and code Playground

by michapixel

JavaScript

const isPrice = (price) => {
	let hasDot = price.indexOf(',') > -1;
	if( hasDot )
		return false;
	let regex  = /^[1-9]\d*(?:\.\d{0,2})?$/;
	if (price.test(regex) ) {
		return true;
	}
	return false;
};

let prices = [
	'2,00',
	'0,5',
	'0,50'
];
const trace = console.log;
prices.forEach((el)=>{
	trace(el, parseFloat(el))
})