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
– weise
You’re wearing a mask, so it indicates an inappropriate shape
– Rafael Augusto
I took this example code Rafael, the mask is indicating an erroneous format? how so? Sorry, I’m starting now in Javascript
– weise
Remove the attribute
pattern="[0-9]+$"
– KaduAmaral
what do you want to do? just have a mask in the input and check if you have enough numbers to be phone?
– Rafael Augusto
That’s really what @Kaduamaral said, I hadn’t seen Pattern, so just remove it. It’s using expression to validate numbers only.
– Rafael Augusto
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!!
– weise