validate several input that its value is greater than 10 (mask)

0

I have a form with many inputs to enter phone, in all its value must have 10 digits I am using a maskara and I add a validate classTelephones to all input for phone, I do not know why I print 16 or 1 in the console .

    $('.validarTelefonos').change(function(){
        var tel = $(".validarTelefonos").val().match( /\d+/g);
      if( tel.length != 10  ){
        console.log( ($('#telefonoParticular').val().length) );
        $('#mensaje').addClass('btn btn-danger').html('El telefono debe tener 10 digitos').show();
        return false;
      } else{
        $('#mensaje').removeClass('btn btn-danger').html('').hide();
      }
    });

maskara

data-inputmask=""mask": "(99) 99-99-99-99""

Look, it's running out but I'm missing each one because of what I think.

            $('.validarTelefonos').change(function(){
            var tel = $(".validarTelefonos").val().match( /\d+/g);
            tel = tel.join("");
          if( tel.length != 10 ){
            console.log( ($('#telefonoParticular').val().length) );
            $('#mensaje').addClass('btn btn-danger').html('El telefono debe tener 10 digitos').show();
            return false;
          } else{
            $('#mensaje').removeClass('btn btn-danger').html('').hide();
          }
        });
    
asked by Carlos Enrique Gil Gil 05.03.2018 в 20:19
source

2 answers

0

Solution

Well that's how it was; Thanks for helping me I added the each to go through all the elements that have the class validarTelefonos

$('.validarTelefonos').change(function(){
  $(".validarTelefonos").each(function(){
     var tel = $(this).val().match( /\d+/g);
     tel = tel.join("");
     if( tel.length != 10 ){
        $('#mensaje').addClass('btn btn-danger').html('El telefono debe tener 10 digitos').show();
        return false;
        } else{
        $('#mensaje').removeClass('btn btn-danger').html('').hide();
            }
        });
    });
    
answered by 05.03.2018 / 22:45
source
1

Explanation in the comments:

data-inputmask=""mask": "(99) 99-99-99-99""
//2 paréntesis       +
//3 guiones          +
//1 espacio en blanco 
//--------------------  
//6 caracteres extra
//Por lo tanto, tienes 10 dígitos + 6 caracteres extras:
//hace un total de 16 caracteres a imprimir.

Fun fact:

On the next line you are always asking about the value of 1 particular field.

console.log( ($('#telefonoParticular').val().length) );

Important detail:

When using a jQuery mask, that format takes "lenght" space in the string where it will be used.

Solution:

Replaces:

if( tel.length != 10  ){

By:

if( tel.length != 16 ){
    
answered by 05.03.2018 в 20:35