Cell Field Mask and Validation

Asked

Viewed 190 times

1

Could you help me with this validation? The "txtTelCel" field should be ###-#########, where # is a numeric digit and, if it is not in this format, a warning should appear informing the user.

Note: without using jQuery.

<label for="txtTelCel">Telefone Celular:</label></div>
<div>
    <input type="text" id="txtTelCel" name="txtTelCel" size="50" maxlength="8">
</div>
  • Have you tried https://igorescobar.github.io/jQuery-Mask-Plugin/ ?

  • I need it to be in Javascript

1 answer

0

Change the attribute maxlenght to 9 and use this function in the onblur.

 function validarTel() {
    var tel = document.getElementById('txtTelCel').value;
    var pattern = /^\d{4}-\d{4}$/;

    if(!pattern.test(tel))
        alert('Número de telefone inválido!');
 }

Browser other questions tagged

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