0

I have price variable and which can contain:

USD12.34 
USD12,34
12,34
23.34

Now the output should be -

USD12.34 - false
USD12,34 - false
12,34 - true
23.34 - true

Now, Using JavaScript I want to validate above data as integer which should accept dot and comma.

I got this code but no luck:

const test = /^\d*(,\d{3})*(\.,\d*)?$/.test( price );
console.log(  ( test ) );
Shibbir
  • 1,963
  • 2
  • 25
  • 48

2 Answers2

1

Here is an example replacing comma with a dot:

// Number with comma rather than decimal:
const numberWithComma = "12000,12";

// Replace commas with decimals and convert to float:
const newNumber = parseFloat(numberWithComma.split(",").join("."));

// Check if number is finite:
const res = Number.isFinite(newNumber);

console.log(res) // true

UPDATE: This next example doesn't use regex, but assuming the limitations you've provided in your updated question, it accomplishes what you're after:

// Function to test number:
const testNumber = (value) => {

  // Declare new variable to hold test value after it is parsed:
  let numberToTest;

  // Test value and assign to numberToTest:
  if (typeof value === "number") {
    numberToTest = value
  } else if (value.startsWith("USD")) {
    const temp = value.split("USD")[1]
    numberToTest = temp.includes(",") ? parseFloat(temp.split(",").join(".")) : parseFloat(temp)
  } else if (value.includes(",")) {
    numberToTest = parseFloat(value.split(",").join("."))
  } else {
    numberToTest = parseFloat(value)
  }

// Test is number is finite and return the boolean:
return Number.isFinite(numberToTest);
}

TSxo
  • 36
  • 4
  • I dont want to replace, I want accept comma and dot. – Shibbir Nov 23 '22 at 04:24
  • Note this only works if the commas in your dataset are for decimal places, fails if commas are used a separators. – TSxo Nov 23 '22 at 04:25
  • If you'd like to accept both, you'll have to parse the content in one manner or another - are the commas being used as thousands separators or for decimal points? – TSxo Nov 23 '22 at 04:29
  • I have updated my question, can you please check it? – Shibbir Nov 23 '22 at 04:41
  • I've updated my answer for you, not a regex solution but it achieves what you're after. – TSxo Nov 23 '22 at 05:11
  • Thank you so much for your answer but maybe i couldn't tell you my exact need. Can you again check my updated question? – Shibbir Nov 23 '22 at 05:14
  • No problems, I tested that function I added against the sample cases and it works on all. You'd just run it against reach price like: `const res = testNumber(price)`. Will return `true` or `false` – TSxo Nov 23 '22 at 05:17
1

const input = [
  'USD12.34',
  'USD12,34',
  '12,34',
  '23.34',
  '12;34'
].forEach(price => {
  const test = /^\d*(?:[.,]\d+)?$/.test(price);
  console.log(price + ' => ' + test);
});

Output:

USD12.34 => false
USD12,34 => false
12,34 => true
23.34 => true
12;34 => false

Explanation:

  • ^ -- anchor at start
  • \d+ -- 1+ digits
  • (?: -- non-capture group start
  • [.,] -- a dot or comma
  • \d+ -- 1+ digits
  • ) -- non-capture group end
  • ? -- make non-capture group optional
  • $ -- anchor at end
Peter Thoeny
  • 7,379
  • 1
  • 10
  • 20