Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I tend to break validation down for each input, which makes this stuff a lot easier. You could also use a jQuery plugin, which would probably be more modular in the long run.</p> <p>That said, I would do it like this (<a href="http://jsfiddle.net/tmvVa/" rel="nofollow">working jsFiddle</a>):</p> <pre><code>//contact form validation $(function() { $('.error').hide(); //hide all errors function fieldHasValue(field, label) { var value = field.val().trim(); //Grab value without leading or trailing whitespace if (value.length &gt; 0) { label.hide(); } else { label.show(); field.focus(); } return (value.length &gt; 0); } function nameIsValid() { return fieldHasValue($("input#contact_name"), $("label#name_error")); } function emailIsValid() { var email = $("input#contact_email").val().trim(); //Grab value without leading or trailing whitespace var emailRegEx = /^([a-zA-Z0-9])(([a-zA-Z0-9])*([\._\-])?([a-zA-Z0-9]))*@(([a-zA-Z0-9\-])+(\.))+([a-zA-Z]{2,4})+$/; var isValid = (email.length &gt; 0) &amp;&amp; email.match(emailRegEx); if (isValid) { $("label#email_error").hide(); $("label#invalid_error").hide(); } else if (email.length &gt; 0) { $("label#invalid_error").show(); $("input#contact_email").focus(); } else { $("label#email_error").show(); $("input#contact_email").focus(); } return isValid; } function phoneIsValid() { var phone = $("input#contact_phone").val().replace(/\s+-/g, "").trim(); //Grab value without leading or trailing whitespace and replace [whitespace -] with an empty string. var phoneRegEx = /^(1-?)?\s?(\([2-9]\d{2}\)|\s?-?[2-9]\d{2})-?\s?[2-9]\d{2}-?\d{4}$/; var isValid = (phone.length &gt; 0) &amp;&amp; phone.match(phoneRegEx); if (isValid) { $("label#invalid_phone_error").hide(); $("label#phone_error").hide(); } else if (phone.length &gt; 0) { $("label#invalid_phone_error").show(); $("input#contact_phone").focus(); } else { $("label#phone_error").show(); $("input#contact_phone").focus(); } return isValid; } function zipIsValid() { return fieldHasValue($("input#contact_zip"), $("label#zip_error")); } function messageIsValid() { return fieldHasValue($("textarea#contact_message"), $("label#message_error")); } function bestTimeIsValid() { return fieldHasValue($("input#contact_best_time"), $("label#best_time_error")); } function allInputsAreValid() { var validName = nameIsValid(); var validEmail = emailIsValid(); var validPhone = phoneIsValid(); var validZIP = zipIsValid(); var validMessage = messageIsValid(); var validBestTime = bestTimeIsValid(); var isValid = validName &amp;&amp; validEmail &amp;&amp; validPhone &amp;&amp; validZIP &amp;&amp; validMessage &amp;&amp; validBestTime; return isValid; } $("input#contact_name").on('change blur', nameIsValid); $("input#contact_email").on('change blur', emailIsValid); $("input#contact_phone").on('change blur', phoneIsValid); $("input#contact_zip").on('change blur', zipIsValid); $("textarea#contact_message").on('change blur', messageIsValid); $("input#contact_best_time").on('change blur', bestTimeIsValid); $("input#contact_button").on('click', function (e) { var timestamp = $("input#timestamp").val(); if (allInputsAreValid()) { var dataString = 'contact_name=' + name + '&amp;contact_email=' + email + '&amp;contact_phone=' + phone + '&amp;contact_zip=' + zip + '&amp;contact_best_time=' + best_time + '&amp;timestamp=' + timestamp + '&amp;contact_message=' + encodeURIComponent(message); $.ajax({ type: "POST", url: "php/contact.php", data: dataString, success: function() { $('#contact-form').html("&lt;div id='message'&gt;&lt;/div&gt;"); $('#message').html("&lt;h3&gt;Message Sent&lt;/h3&gt;").append("&lt;p&gt;Thank you for contacting us. We'll be in touch. &lt;br /&gt;&lt;br /&gt;If you have any further questions, you can always email us at info@wirecrafters.com or call us at 800-924-9473&lt;/p&gt;").hide().fadeIn(800, function() { $('#message').append("&lt;img id='checkmark' src='images/submit2.png' /&gt;"); }); } }); } e.preventDefault(); return false; }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload