简体   繁体   中英

Unable to validate form on submit

I'm using a small library called liveValidation.js. I'm using this to validate a couple of inputs in a form.

It should automatically disable the form button if there's some invalid inputs, but it doesn't seam to work.

Here's my HTML code:

<form method="POST" id="contactForm">
     <label for="name">Name</label>
     <input type="text" name="name" id="contactFormName" value=""/>
     <label for="email">E-Mail</label>
     <input type="text" name="email" id="contactFormEmail" value=""/>
     <label for="message">Your message here</label>
     <textarea name="message" id="contactFormMessage"></textarea>
     <input type="submit" id="submit" value="submit"/>
</form>

Here's how I initialize liveValidation.js:

function liveValidation() {
    var name = new LiveValidation('contactFormName');
    name.add(Validate.Presence);
    var email = new LiveValidation('contactFormEmail');
    email.add(Validate.Presence);
    email.add(Validate.Email);
    var message = new LiveValidation('contactFormMessage');
    message.add(Validate.Presence);
};
$(document).ready(function ($) {
    $("#loadingDiv").hide(400);
    liveValidation();
    sendEmail();
});

and this is the AJAX request code:

function sendEmail() {
    var form = $("#contactForm");
    var resultDiv = $(".formResult");
    $("#submit").click(function () {
    $.ajax({
        type: "POST",
        url: "sendEmail.php",
        data: form.serialize()
        }).done (function (){
          resultDiv.addClass('success').html('Message sent successfully')
        }).fail(function () {
          resultDiv.addClass('fail').html("Message not sent. Try again")
        });
     }
  });
};

Any thought why this is not working properly? Here's the livevalidation website if it could help -> http://livevalidation.com/

You need to check manually if the form is valid. To do that you need one (any one) of the LiveValidation objects

Try this

$(document).ready(function ($) {
    var obj = liveValidation();
   sendEmail(obj);
});

function liveValidation() {
  var name = new LiveValidation('contactFormName');
  name.add(Validate.Presence);
  var email = new LiveValidation('contactFormEmail');
  email.add(Validate.Presence);
  email.add(Validate.Email);
  var message = new LiveValidation('contactFormMessage');
  message.add(Validate.Presence);

  return name;
};

function sendEmail(obj) 
{
    var automaticOnSubmit = obj.form.onsubmit;
    $("#submit").click(function () {
      var valid = automaticOnSubmit();
          if(!valid)
            {
                alert('The form is not valid!');
                 event.preventDefault();
            }
            else
            {
                //submi form
            }
   });
 }

An alternate and better way, you can use any LiveValidation object to attach the event

$(document).ready(function ($) {
    liveValidation();
    sendEmail();
});

function liveValidation() {
  var name = new LiveValidation('contactFormName');
  name.add(Validate.Presence);
  var email = new LiveValidation('contactFormEmail');
  email.add(Validate.Presence);
  email.add(Validate.Email);
  var message = new LiveValidation('contactFormMessage');
  message.add(Validate.Presence);

  var automaticOnSubmit = name.form.onsubmit;
  name.form.onsubmit = function(){
      var valid = automaticOnSubmit();
      if(valid)
          alert('The form is valid!');
      else
          alert('The form is not valid!');
      return false;
  }

};

function sendEmail() 
{

    $("#submit").click(function () {
       //submit form here
   });
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM