Validating Javascript fields

Asked

Viewed 1,195 times

1

I have a little problem in a code that transforms the Phone field to this format: (51) 96321-5465. However, every time I click Submit, the form says in the field: "É preciso que o formato corresponda ao exigido." and does not send the form, the code is this:

<!-- Formatar campo telefone -->
<script type="text/javascript">
/* Máscaras ER */
function mascara(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}
function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}
function mtel(v){
    v=v.replace(/\D/g,"");             //Remove tudo o que não é dígito
    v=v.replace(/^(\d{2})(\d)/g,"($1) $2"); //Coloca parênteses em volta dos dois primeiros dígitos
    v=v.replace(/(\d)(\d{4})$/,"$1-$2");    //Coloca hífen entre o quarto e o quinto dígitos
    return v;
}
function id( el ){
    return document.getElementById( el );
}
window.onload = function(){
    id('telefone').onkeypress = function(){
        mascara( this, mtel );
    }
}
</script> <!-- FIM Formatar campo telefone -->

And where I want the field to appear to inform the phone:

< div class="campo">
    < label for="telefone">Telefone</label>
    < input type="text" id="telefone" name="telefone" placeholder="Somente números" size="16" maxlength="15"  value="" pattern="[0-9]+$"/>
< /div>

Porr that keeps showing this message and does not let send even the field not having a required?

  • I thought it was something with "size="16" maxlength="15"" but it’s not

  • You’re wearing a mask, so it indicates an inappropriate shape

  • I took this example code Rafael, the mask is indicating an erroneous format? how so? Sorry, I’m starting now in Javascript

  • Remove the attribute pattern="[0-9]+$"

  • what do you want to do? just have a mask in the input and check if you have enough numbers to be phone?

  • That’s really what @Kaduamaral said, I hadn’t seen Pattern, so just remove it. It’s using expression to validate numbers only.

  • Muuuuuuuuuuuuuuuuuuuito thanks for the personal help, that’s all, I had thought about it, but I thought if I removed it, it would add letters and other characters, which does not happen due to the functions created on top of js, again, THANK YOU!!

Show 2 more comments

2 answers

2

DEMO

No need to remove attribute pattern but to suit him.

(^[\d-\)\(]+$)

HTML

<div class="campo">
<label for="telefone">Telefone</label>
<input type="text" id="telefone" name="telefone" placeholder="Somente números" size="16" maxlength="15"  value="" pattern="(^[\d-\)\(]+$)"/>

  • \d dígito o mesmo que [0-9]
  • -\)\( caracteres aceitos
  • Really works Leo Caracciolo, thank you so much for your contribution, very grateful, have a great afternoon.

1


You are using the attribute pattern to validate the data of input. Removing it will be all right, and validation will occur only via Javascript:

<div class="campo">
    <label for="telefone">Telefone</label>
    <input type="text" id="telefone" name="telefone" placeholder="Somente números" size="16" maxlength="15"  value="">
</div>

Browser other questions tagged

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