lundi 18 avril 2016

Jquery & Ajax Form Validation function

I'm trying to find a way to do a form validation with Jquery. I currently have a problem with the validation function. I am able to send the form without having anything in the input fields. Could you please tell me the way to make it work.

 function Valid(){
    var name1 = false;
    var name2 = false;
    var num = false;
    var mail = false;
    var msg = false;
   
     if (document.myForm.name.value ==="") {
          $('#name1').addClass('has-error');
          $( "#name2" ).click(function(){$("#name1").removeClass('has-error');});
          name1 = false;}
        
     if (!/^[a-zA-Z]*$/g.test(document.myForm.name.value)) {
          $('#name1').addClass('has-error');
          $('#name2').click(function(){$('#name1').removeClass('has-error');});
          name2 = false;}
    
    if (document.myForm.num.value.length < 17){
          $('#num1').addClass('has-error');
          $( "#num2" ).click(function(){$("#num1").removeClass('has-error');});
          num = false;}
    
    if (document.myForm.email.value === "") {
          $('#email1').addClass('has-error');
          $( "#email2" ).click(function(){$("#email1").removeClass('has-error');});
           mail = false;}
    
    if (document.myForm.message.value === "") {
          $('#msg1').addClass('has-error');
          $( "#msg2" ).click(function(){$("#msg1").removeClass('has-error');});
           msg = false;} 
  }// END VALIDATION
  
  
   function SubmitFunction(){
          if(Valid()){
          $.ajax({
              type: "POST",
              url:"../php/process.php",
              data: $('form.contact').serialize(),
              success: function(msg){
                $("#myModal").modal('hide');
                $("#msgsuccess").delay(500).fadeIn(500).delay(1000).fadeOut(500);
                }    
          });//End 
         }
        }//End SubmitFunction

Thank you in advance !!




Aucun commentaire:

Enregistrer un commentaire