timeout = false; values = new Array(); values["USD"] = "1.32420"; values["EUR"] = "0.75520"; function convert(from, to) { timeout = setTimeout(function(){ if ($("#from_" + from).val() != "") { if (!($("#from_" + from).val() > 0)) { alert("Debe ingresar un numero") $("#from_" + from).val("1"); $("#from_" + from).css("border", "2px solid red"); } else { $("#to_" + to).val(""); $("#from_" + from).css("border", "1px solid black"); } conv = values[to]*$("#from_" + from).val(); if (conv > 1000) { conv = number_format(conv, 0, ',', '.'); } else { conv = number_format(conv, 2, ',', '.'); } $("#to_" + to).val(conv); } else { $("#to_" + to).val(""); } }, 100); } $(document).ready(function() { $('#from_EUR').keydown( function() { convert("EUR", "USD"); } ); $('#from_USD').keydown( function() { convert("USD", "EUR") } ); convert("EUR", "USD"); convert("USD", "EUR"); }); /* Made by Mathias Bynens */ function number_format(a, b, c, d) { a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b); e = a + ''; f = e.split('.'); if (!f[0]) { f[0] = '0'; } if (!f[1]) { f[1] = ''; } if (f[1].length < b) { g = f[1]; for (i=f[1].length + 1; i <= b; i++) { g += '0'; } f[1] = g; } if(d != '' && f[0].length > 3) { h = f[0]; f[0] = ''; for(j = 3; j < h.length; j+=3) { i = h.slice(h.length - j, h.length - j + 3); f[0] = d + i + f[0] + ''; } j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3)); f[0] = j + f[0]; } c = (b <= 0) ? '' : c; return f[0] + c + f[1]; }