can characters be added by default to an input text?

0

There is some way that when you enter a value into the input a character is added as a point or comma, it is very similar to what the input date does with the "/" and when your input is accommodated with the "/", this can be applied in an input text?

    
asked by ingswsm 19.06.2017 в 17:21
source

1 answer

0

The answer to your question is: Yes, if you can add characters by default to an input text.

In the comments you can see an example of how to add characters in different parts of the input to give a specific format to the text input.

If you only want to add characters, as your question indicates, it will be enough to do something like the following:

Some examples using JQuery which is the most common:

Add at the end:

$("#id-input").on("input",function(){
    var v = $(this).val();
    $(this).val(v+"x");//Puedes reemplazar "x" con cualquier otro caracter.
});

Add a point to have "two decimals":

$("#id-input").on("input",function(){
    var v = $(this).val();
    if(v.toString().length > 2)
    {
        if(v.indexOf(".") > 0)
            v = v.slice(0,v.indexOf("."))+v.slice(v.indexOf(".")+1); 
        $(this).val(v.slice(0,-2)+"."+v.slice(-2));
    }

});

Depending on the format you need, you can add the characters you need.

    
answered by 19.06.2017 в 18:10