{"id":3285,"date":"2011-03-14T01:58:36","date_gmt":"2011-03-13T20:28:36","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=3285"},"modified":"2011-03-14T02:26:08","modified_gmt":"2011-03-13T20:56:08","slug":"simple-client-side-ajax-validation","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/simple-client-side-ajax-validation\/","title":{"rendered":"Simple Client Side Ajax Validation"},"content":{"rendered":"<div id=\"_mcePaste\">Well my second month into work and I have started discovering things that are very trivial, but can save you a whole lot of time .. if you could get to know them better .<\/div>\n<div id=\"_mcePaste\">One of those things was with jQuery.<\/div>\n<div id=\"_mcePaste\">Now working with a normal submit form, a <strong>jQuery Validate Plugin<\/strong> works superbly. You simply give<\/div>\n<div id=\"_mcePaste\">&#8220;<strong>class = required <\/strong>&#8220;<\/div>\n<div id=\"_mcePaste\">and it does the job.<\/div>\n<p>But when do the same with an Ajax call .. the Ajax call goes out without a validation.. which is what you don&#8217;t want. The call should only go out if the validation passes . Now faced with this I started to write a custom method &#8230; but on some reading up, found out that there actually exists a way to do this through the Plugin itself.<\/p>\n<div>The <strong>.form()<\/strong> method coupled with a <strong>.validate<\/strong> gives you a Boolean value i.e true if validated or false if not.<\/div>\n<p>Something like<\/p>\n<p><span style=\"font-family: Consolas, Monaco, 'Courier New', Courier, monospace; font-size: 12px; line-height: 18px; white-space: pre;\"> <\/span><\/p>\n<p>[javascript]<br \/>\nfunction validateForm() {<br \/>\n.<br \/>\n.<br \/>\n  if ($(&#8216;#form&#8217;).validate().form()) {<br \/>\n          \/\/ (Put your Ajax Call here)<br \/>\n    }<br \/>\n}<br \/>\n[\/javascript]<\/p>\n<div id=\"_mcePaste\">In this way the Ajax call went out only if the<strong> .validate().form()<\/strong> returned true.<\/div>\n<div id=\"_mcePaste\">Another usecase where I used it was when the whole form was when the whole form went through an Ajax call.<\/div>\n<div id=\"_mcePaste\">I used a <strong>&lt;g:formRemote&gt;<\/strong> tag . Now it doesn&#8217;t have a &#8216;<strong>onSubmit<\/strong>&#8216; of the normal form but it does have a &#8216;<strong>before<\/strong>&#8216; attribute that works somewhat similarly.<\/div>\n<div id=\"_mcePaste\">So now,<\/div>\n<p>[html]<br \/>\n&lt;g:formRemote name=&quot;searchPage&quot; id=&quot;searchPage&quot; url=&quot;${[controller:&#8217;eventInstance&#8217;,action:&#8217;executeSearch&#8217;]}&quot; update=&quot;searchUpdate&quot; before=&quot;return validateSearchForm()&quot;&gt;<br \/>\n.<br \/>\n.<br \/>\n&lt;\/g:formRemote&gt;<br \/>\n[\/html]<\/p>\n<p>With a Javascript function<\/p>\n<p>[javascript]<br \/>\nfunction validateSearchForm() {<br \/>\n    if ($(&#8216;#searchPage&#8217;).validate().form() &amp;&amp; checkDate()) {<br \/>\n      return true;<br \/>\n    }<br \/>\n    else {<br \/>\n      return false;<br \/>\n    }<br \/>\n  }<br \/>\n[\/javascript]<\/p>\n<p>So in this way the page would be Ajaxified if both of those methods returned true &#8230;<\/p>\n<p>Hope this helps ..<\/p>\n<p>Manoj Mohan<br \/>\n<strong>Manoj (at) Intelligrape (dot) com<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Well my second month into work and I have started discovering things that are very trivial, but can save you a whole lot of time .. if you could get to know them better . One of those things was with jQuery. Now working with a normal submit form, a jQuery Validate Plugin works superbly. [&hellip;]<\/p>\n","protected":false},"author":32,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3285"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/32"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=3285"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3285\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=3285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=3285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=3285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}