ajax - Custom error headings for form validation doesn't display properly -
my contact form uses ajax, however, when try validate form without filling in of fields, 3 out of 4 error messages display. 'last name', 'email address' , 'message' error headings display, 'first name' error heading not. when take out 'last name' error field , retest, 'first name' field displays.
i have tried expanding box make large, thinking there no room 4 error headings display, don't think case. how can correct 4 error messages (headings) display if user not fill out of fields?
here code:
$(document).ready(function() { $('form #response').hide(); $('.button').click(function(e) { e.preventdefault(); var valid = ''; var required = ' required.'; var first = $('form #first').val(); var last = $('form #last').val(); var email = $('form #email').val(); var message = $('form #message').val(); var tempt = $('form #tempt').val(); var tempt2 = $('form #tempt2').val(); if (first = '' || first.length <= 1) { valid = '<p>your first name' + required + '</p>'; } if (last = '' || last.length <= 1) { valid = '<p>your last name' + required + '</p>'; } if (!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) { valid += '<p>your email address' + required + '</p>'; } if (message = '' || message.length <= 4) { valid += '<p>a message' + required + '</p>'; } if (tempt != 'http://') { valid += '<p>we can\'t allow spam bots.</p>'; } if (tempt2 != '') { valid += '<p>a human user' + required + '</p>'; } if (valid != '') { $('form #response').removeclass().addclass('error') .html('' +valid).fadein('fast'); } else { $('form #response').removeclass().addclass('processing').html('sending...').fadein('fast'); var formdata = $('form').serialize(); submitform(formdata); } }); }); function submitform(formdata) { $.ajax({ type: 'post', url: 'mail/mailform.php', data: formdata, datatype: 'json', cache: false, timeout: 4000, success: function(data) { $('form #response').removeclass().addclass((data.error === true) ? 'error' : 'success') .html(data.msg).fadein('fast'); if ($('form #response').hasclass('success')) { settimeout("$('form #response').fadeout('fast')", 4000); } }, error: function(xmlhttprequest, textstatus, errorthrown) { $('form #response').removeclass().addclass('error') .html('<p>there <strong>' + errorthrown + '</strong> error due <strong>' + textstatus + '</strong> condition.</p>').fadein('fast'); }, complete: function(xmlhttprequest, status) { $('form')[0].reset(); } }); };
html ----------->>
<form class="contact-me" action="mail/mailform.php" method="post" name="contact-me"> <div id="response"><!-----------------contains form error message--------------></div> <input name="first" class="required" title="your first name required" id="first" autofocus placeholder="first name" type="text" maxlength="15"></input><br> <input name="last" title="your last name required" id="last" placeholder="last name" type="text" maxlength="15"></input><br> <input name="e-mail" title="e-mail address required" id="email" placeholder="email address" type="email" maxlength="50"></input><br> <textarea name="message" title="comments required" placeholder="comments" rows="1" cols="50" id="message" onkeydown="textcounter(this.form.comments,this.form.countdisplay);"onkeyup="textcounter(this.form.message,this.form.countdisplay);"></textarea><br> <input type="hidden" name="tempt" id="tempt" value="http://" /> <input type="hidden" name="tempt2" id="tempt2" class="clear" value="" /> <input name="countdisplay" type="text" id="countdisplay" value="2000" size="4" maxlength="4" readonly></input><br> <button class="button" ontouchstart="('touchstart');">send</button> </form>
you should type valid += "validation message"
instead of valid = "validation message"
each validation. forgot +=
first name , last name validations.
Comments
Post a Comment