WST.onlyPriceNum = function (obj) { var priceRegex = /^\d+\.?\d{0,2}$/; obj.blur(function(event) { var event = event || window.event; var val = $(this).val(); var newVal = val.replace(/[^\d.]/g, ""); newVal = newVal.replace(/^\./g, ""); newVal = newVal.replace(/\.{2,}/g, "."); newVal = newVal.replace(".", "$#$").replace(/\./g, "").replace("$#$", "."); newVal = newVal.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); $(this).val(newVal); }); obj.keyup(function(event) { var event = event || window.event; var val = $(this).val(); var newVal = val.replace(/[^\d.]/g, ""); newVal = newVal.replace(/^\./g, ""); newVal = newVal.replace(/\.{2,}/g, "."); newVal = newVal.replace(".", "$#$").replace(/\./g, "").replace("$#$", "."); if (newVal.length > 0) { var valArray = newVal.split("."); var index = newVal.indexOf("."); if (valArray.length === 1) { if (index !== -1) { newVal = parseInt(valArray[0]) + "."; } else { newVal = parseInt(valArray[0]) + ""; } } else { newVal = parseInt(valArray[0]) + "." + valArray[1]; } } newVal = newVal.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); $(this).val(newVal); }); obj.keypress(function(event) { if (event.charCode !== 46 && event.charCode !== 0 && event.charCode < 48 || event.charCode > 57) { return false; } if (/\./.test($(this).val()) && event.charCode === 46) { return false; } if ($(this).val() === "" && event.charCode === 46) { return false; } var tails = ($(this).val()).split(".")[1]; if (tails && tails.length === 2 && event.charCode !== 0) { return false; } }); }