start with 9 in front with jquery

0

I have this code that gives me the phone mascara, but I can not make the input already start with 9 in front, preventing the user from erasing it, making it mandatory. Does anyone have an idea what to do?

<script>
    if (opt == "Celular") {
        if (document.getElementById('fone').readOnly)
            document.getElementById('fone').readOnly = false;

        mask = "99999-9999";
        document.getElementById("oc_oc").className = "oculto";
    }
</script>

<div class="num_class">
    <label>Numero</label><br>
    <input readonly maxlength="10" required class="inp_editar" type="text" name="fone_tel" id="fone"/>
</div>
    
asked by anonymous 26.03.2018 / 15:31

1 answer

1

You can do something like this

$("input").keydown(function(e) {
    var oldvalue=$(this).val();
    var field=this;
    setTimeout(function () {
        if(field.value.indexOf('9') !== 0) {
            $(field).val(oldvalue);
        } 
    }, 1);
});

Here an example working

I used jquery which can be downloaded here

subistitua input por #seuId ou .suaClas

    
26.03.2018 / 15:42