DOUBT To validate form with jQuery before "Check Data"

1

I'm seeing how I can validate a form with jQuery.

The form can be of "4 types" with what depending on the option that you choose at the beginning:

  • "I would like to add a required attribute to the required fields, of course it will vary depending on which option the user chooses at the beginning."

The required, I add it in this way

$(function(){
       $("#idcampo").prop('required',true);
});

When the client fills everything, you can press a button that says: "Check Data"

  • This is where I would like to validate, before displaying a modal with a summary before sending, so that if all the "required" are filled in, then if it shows the modal with the summary.

  • If the required fields are not complete, mark them in red, this is not a problem, since I would add some class.

I think I've explained myself correctly, if someone thinks they do not understand my doubt, let me know so I can explain it better.

PS: The modal, I have it enabled in jQuery, simply, .show ()

Thanks in advance to the community. Greetings.

    
asked by Javier Avila Fernandez 22.08.2018 в 11:58
source

1 answer

1

Do a Validation using Jquery, depending where, it may be insecure, I recommend that you validate in PHP, if you know that the use of your form is of "exclusive" use for certain people, continuous, instead of the button being validated data can be called Send do in it a onclick="enviar_fomulario();" there you can validate the form by calling within that function to another function that is function validar_datos(<campos que quieras validad>) the thing more or less serious like this.

function enviar_formulario(){
  //puedes primero comprobar si los valores de los campos se recogen con un console.log()
  var campos = {$('#id_campo1').val(), $('#id_campo2').val()...}

  if(validar_datos(campos) == true){
    document.forms.<nombre_del_formulario>.submit();
  }else{
    //haces un alert o lo que sea
  }
}

function validar_datos(campos){
  for(var i in campos){
    if(campos[i] == ""){
      return false;
    }else{
      return true;
    }
  }
}

I do not know if with this you clear the question, but more or less serious so what you ask surely have to change something but good,

A greeting

    
answered by 22.08.2018 / 12:20
source