start with 9 in front with jquery

Asked

Viewed 26 times

0

I have this code that gives me the phone mask, but I can’t get the input to start with the 9 in front, preventing the user to delete it, making it mandatory. Does anyone have any 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>

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 a working example

I used jquery which can be downloaded here

subistitua input por #seuId ou .suaClas

  • In my case it did not work because it interferes with the first function, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.