0

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">

1 Answer 1

1

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,'');
    }        
}   
Sign up to request clarification or add additional context in comments.

2 Comments

The issue now is how to remove the format to manipulate the number... ¿any suggestion?
@vyplow The second line does it: input.value.substring(1, input.value.length)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.