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 ) );
4
  • replace comma with dot? Commented Nov 23, 2022 at 4:13
  • I dont want to replace, I want accept comma and dot. Commented Nov 23, 2022 at 4:22
  • I have updated my question, can you please check it? Commented Nov 23, 2022 at 4:41
  • /^\d+([,.]\d+)?$/ Commented Nov 23, 2022 at 5:17

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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);
}

7 Comments

I dont want to replace, I want accept comma and dot.
Note this only works if the commas in your dataset are for decimal places, fails if commas are used a separators.
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?
I have updated my question, can you please check it?
I've updated my answer for you, not a regex solution but it achieves what you're after.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.