2

I have to match values like

€ 6.483,00

OR values like

18,50%  

OR, again,

+65,86 %

in a Javascript function, which I drafted as:

function(s) {
  return /^[0-9]?[0-9,\.]*$/.test(s);
}

but, obviously, it's not working... how should it be changed?

Zach Saucier
  • 24,871
  • 12
  • 85
  • 147
Fabio B.
  • 9,138
  • 25
  • 105
  • 177
  • Have a read of http://stackoverflow.com/questions/3079582/problem-using-jquery-calculation-with-european-formatted-numbers – mplungjan Sep 12 '11 at 08:41
  • nice but... it's not an answer, replacing /(-|-\$)?(\d+(\.\d{3})*(\,\d{1,})?|\,\d{1,})?/g in the above function still doesn't get my job! – Fabio B. Sep 12 '11 at 08:44

2 Answers2

3
^(?:€|\+)?\s*\d+(?:\.?\d{3})*(?:,\d+)?\s*%?\s*$

See it here on Regexr

Start of the string, an optional € or +, then optional whitespace, Then there should be at list one digit, followed by an optional dot and three digits, then an optional fraction, optional whitespace, optional % more optional whitespace and then the end of the string,

stema
  • 90,351
  • 20
  • 107
  • 135
2
var sample = [
    "€ 6.483,00",
    "18,50%",
    "+65,86 %"
]
for(var i = 0; i < sample.length; i++) {
    var input = sample[i];
    var regex = /^(\u20ac ?)?\+?\d+(\.\d+)?(\,\d+)?( ?%)?$/
    console.log(input + "\t" + regex.test(input));
}

If there are cases that do not/should not match then let me know.

Salman A
  • 262,204
  • 82
  • 430
  • 521