How to force an HTML form to validate without submitting it via jQuery

To check whether a certain field is valid, use: $(‘#myField’)[0].checkValidity(); // returns true|false To check if the form is valid, use: $(‘#myForm’)[0].checkValidity(); // returns true|false If you want to display the native error messages that some browsers have (such as Chrome), unfortunately the only way to do that is by submitting the form, like this: … Read more

Merge values from two forms on submit

jQuery serialize supports multiple form elements, So it is possible to do: $(‘#form1, #form2’).serialize(); And for your case, you can do: $(‘#form1’).submit(function() { var action = $(this).attr(‘action’); if (!EntryCheck()) return false; $.ajax({ url : action, type : ‘POST’, data : $(‘#form1, #form2’).serialize(), success : function() { window.location.replace(action); } }); return false; });

Validating url with jQuery without the validate-plugin?

You can use the same regex that the validation plugin does (updated to latest regex on May 23rd, 2015): function isUrlValid(url) { return /^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&’\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&’\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&’\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&’\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&’\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(url); } var testCases = [ “http://www.google.com/”, “https://www.google.com/”, “ftp://www.google.com/”, “http://google.com/”, “http://google.com”, “https://google.com”, “http://www.google.com:80/”, “https://www.google.com:443/”, “http://127.0.0.1/”, “http://127.0.0.1:9200/”, “www.site.com”, “x:”, “http://”, “javascript:alert(‘xss’)”, “http://w”, “http:”, “derp://www.google.com”, “http://localserver” ], div = document.getElementById(“output”); for(var i=0; i < … Read more

jQuery.post( ) .done( ) and success:

jQuery used to ONLY have the callback functions for success and error and complete. Then, they decided to support promises with the jqXHR object and that’s when they added .done(), .fail(), .always(), etc… in the spirit of the promise API. These new methods serve much the same purpose as the callbacks but in a different … Read more

Draggable div without jQuery UI

Here’s a really simple example that might get you started: $(document).ready(function() { var $dragging = null; $(document.body).on(“mousemove”, function(e) { if ($dragging) { $dragging.offset({ top: e.pageY, left: e.pageX }); } }); $(document.body).on(“mousedown”, “div”, function (e) { $dragging = $(e.target); }); $(document.body).on(“mouseup”, function (e) { $dragging = null; }); }); Example: http://jsfiddle.net/Jge9z/ I understand that I shall … Read more

jQuery: Test if checkbox is NOT checked

One reliable way I use is: if($(“#checkSurfaceEnvironment-1”).prop(‘checked’) == true){ //do something } If you want to iterate over checked elements use the parent element $(“#parentId”).find(“checkbox”).each(function(){ if ($(this).prop(‘checked’)==true){ //do something } }); More info: This works well because all checkboxes have a property checked which stores the actual state of the checkbox. If you wish you … Read more

What is the difference between XMLHttpRequest, jQuery.ajax, jQuery.post, jQuery.get

XMLHttpRequest is the raw browser object that jQuery wraps into a more usable and simplified form and cross browser consistent functionality. jQuery.ajax is a general Ajax requester in jQuery that can do any type and content requests. jQuery.get and jQuery.post on the other hand can only issue GET and POST requests. If you don’t know … Read more