Mask and Field Validation for Mobile

1

Could you help me with this validation? The field "txtTelCel" should have the format #### - ####, where # is a numeric digit and if it is not in that 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>
    
asked by anonymous 13.02.2016 / 22:31

1 answer

0

Change the attribute maxlenght to 9 and use this function in 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!');
 }
    
14.02.2016 / 14:42