Reputation: 69
I need to know how can I append $ sign at the start of input text field like $8.000
I am using the below javascript code for the same .
function autofild(input){
var num = input.value.replace(/\./g,'');
if(!isNaN(num)){
num = num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1.');
num = num.split('').reverse().join('').replace(/^[\.]/,'');
input.value = num;
}else{
input.value = input.value.replace(/[^\d\.]*/g,'');
}
}
HTML
<input id="vida_ingreso" onkeyup="autofild(this)" type="text">
Upvotes: 0
Views: 116
Reputation:
Do you mean this?
function autofild(input){
var num = input.value.substring(1, input.value.length);
num = num.replace(/\./g,'');
if(!isNaN(num)){
num = num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1.');
num = num.split('').reverse().join('').replace(/^[\.]/,'');
input.value = '$' + num;
}else{
input.value = '$' + input.value.replace(/[^\d\.]*/g,'');
}
}
Upvotes: 1