I'm attempting to creating a numerical field in a form -- the field automatically converts numbers to currency. Everything works fine, except for the 'minus' sign. It seems like a simple fix, but escaping with regex in javascript/jquery doesn't seem to work/I'm not applying it correctly. Here's my code:
function format() {
// unformat the value
var value = this.value.replace(/[^0-9\.-]+/g, "");
var on = /^(\d{1,3})((?:\d{3})*)$/.exec(value);
var commas = on[2].replace(/(\d{3})/g, ',$1');
this.value = '$' + [on[1], commas, on[3]].join('');
}
$(document).ready(function() {
$('.we').on('keyup', format);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<input type="text" class='we' />
<input type="text" class='we' />
Thanks for assistance,