Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is commonly achieved with the <code>keyup</code> function. Whenever a key is released, a function can be called to make sure that all the fields are accurate, and if so, left the user know in an unobtrusive way. In your case, you could have the submit button disabled unless all the fields are correct.</p> <p>Note: You should never modify currently edited text fields with the function, or do anything else that will prove to be an annoyance to the user ;)</p> <hr> <p>Edit: I modified the code to validate every time a key is lifted for all input fields. I'm not fluent in JQuery, so I did it with vanilla Javascript. If need be, it should be pretty easy to convert.</p> <pre><code>$(function(){ var inputs = document.getElementsByTagName("input"); for(var i = 0; i &lt; inputs.length; i++) { inputs[i].addEventListener("keyup", function(){validate()}, true); } $('.error').hide(); $(".cool-button").click(function(){validate()}); function validate() { $('.error').hide(); $("label#name_error").hide(); $("label#invalid_error").hide(); $("label#email_error").hide(); $("label#invalid_phone_error").hide(); $("label#phone_error").hide(); $("label#zip_error").hide(); var name=$("input#contact_name").val(); var email=$("input#contact_email").val(); var emailRegEx =/^([a-zA-Z0-9])(([a-zA-Z0-9])*([\._-])?([a-zA-Z0-9]))*@(([a-zA-Z0-9\-])+(\.))+([a-zA-Z]{2,4})+$/ var phone=$("input#contact_phone").val(); var phone=phone.replace(/\s+-/g, ""); var phoneRegEx = /^(1-?)?\s?(\([2-9]\d{2}\)|\s?-?[2-9]\d{2})-?\s?[2-9]\d{2}-?\d{4}$/ var zip=$("input#contact_zip").val(); var best_time=$("input#contact_best_time").val(); var message=$("textarea#contact_message").val(); var timestamp=$("input#timestamp").val(); if(name==""){ $("label#name_error").show();$("input#contact_name").focus();return false; } if(email==""){ $("label#email_error").show();$("input#contact_email").focus();return false; } if (document.getElementById('contact_email').value.search(emailRegEx )==-1) { $("label#invalid_error").show();$("input#contact_email").focus();return false; } if(phone==""){ $("label#phone_error").show();$("input#contact_phone").focus();return false; } if (document.getElementById('contact_phone').value.search(phoneRegEx )==-1) { $("label#invalid_phone_error").show();$("input#contact_phone").focus();return false; } if(zip==""){ $("label#zip_error").show();$("input#contact_zip").focus();return false; } if(message==""){ $("label#message_error").show();$("textarea#contact_message").focus();return false; } else { var dataString='contact_name='+name+'&amp;contact_email='+email+'&amp;contact_phone='+phone+'&amp;contact_best_time='+best_time+'&amp;contact_zip='+zip+'&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;");});}}); } return false; }; }); </code></pre>
    singulars
    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.
 

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