<input onblur="this.value = this.value.replace(/[^\d]/g, '').replace(/\B(?=(?:\d{3})+(?!\d))/g, ' ')" name="cost" value="" onfocus="this.value = this.value.replace(/\s/g, '')" >