I am trying to get my form for validation before using an Ajax request to submit the form to my php script. I looked through stackoverflow and did not find something that worked. I'm still new to jQuery, so naked with me.
I have 3 inputs and a submit button:
<input type="text" name="full-name" id="full-name" value="" placeholder="Full Name" /> <input type="text" name="email" id="email" value="" placeholder="Email" /> <textarea name="message" id="message" value="" rows="8" placeholder="Message"></textarea> <input type="submit" id="contact-submit" class="contact-submit" value="Submit"> <div id="messageResponse" class="center" style="display:none;"></div> $(document).ready(function() { function validator(){ return $('form').validate(); } $('form').on('submit', function(e) { e.preventDefault(); $.ajax({ dataType: 'html', type: 'post', url: 'mail.php', data: $('#contact-form').serialize(), beforeSubmit: validator, success: function(responseData) { $('#contact-submit').remove(); $('#messageResponse').fadeIn(1000); $('#messageResponse').html(responseData); }, error: function(responseData){ console.log('Ajax request not recieved!'); } }); // resets fields $('input#full-name').val(""); $('input#email').val(""); $('textarea#message').val(""); }) });
Should I use a plugin for this? All I want is a name, email address and verification message with a red border. This gives me a big headache, and I will meet with the client tomorrow to complete the site. I do not ask if I can find a working solution.
I also tried using this: jQuery Form Validator Plugin
source share