Currency RegExp

by Leandro Barbosa

HTML

<p>yo, check the console</p>

JavaScript

// Currency US/EU
// Matches: check console

var arr = ['a', 1, 1.11, '1,11', '1,111.11', '1.111,11', -1, -1.11, '-1,11', '-1,111.11', '-1.111,11'];

var regexp = /^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{2})?|(?:,[0-9]{3})*(?:\.[0-9]{2})?|(?:\.[0-9]{3})*(?:,[0-9]{2})?)$/;

function isCurrency(str) {
    console.log(regexp.test(str));
    return regexp.test(str);
}

for (var i = 0, l = arr.length; i < l; i++) {
    isCurrency(arr[i]);
}