Buy Now
Sign in

Custom Validation

Crams , August 07 at 00:27
Answered
C
Crams
Hi again! I've been breaking my head with this, I have this custom validation that I know for a fact is getting the right value from the data_read (true or false) but since ajax is asynchronous the validation fails to get the value...
jQuery.fn.form.settings.rules.fmnem = function(value){​
​ var clientx = jQuery("select#client option:checked").val();​
​ jQuery.ajax({​
​ url: '{url:search}&tvout=view',​
​ data: {client:clientx, nem:value},​
​ success: function(result){​
​ console.log("Return: "+result);​
​ return result;​
​ }​
​ });​
​};
I saw in a lot of posts recommendations about using ajax to run a data_read but even if I use "async:false" the value is not returned hence the validation fails, I don't know any other way to fetch data from the db to a js function... all I need is to check if the string written by the user is already in the db to prevent a duplicate, (I know this could be done on the submit but that's not the idea)...
Any suggestions?
HELP PLEASE!!!
C
Crams
Any ideas? Anyone?
healyhatman
jQuery.fn.form.settings.rules.customfn= function(value, param){​
​ var res = true; <---- Declare your result here before your ajax call​
​ jQuery.ajax({​
​ url: '{url:validate}&tvout=view',​
​ async:false, <--- make sure async is false​
​ type: "POST",​
​ data: {val:value},​
​ success: function(result){​
​ res = true;​
​ },​
​ fail: function(result) {​
​ res = false;​
​ }​
​ });​
​ return res; <--- Return your result here​
​};
Web developer at SkySpider.com.au
I don't work for ChronoEngine but I do accept donations paypal.me/healyhatman
C
Crams
Hey Healy!! Sorry I didn't answer before! been away from the keyboard for a while, I really appreciate your help, you are awesome! but the problem is that browsers don't support "async false" because it may cause a hangup, so it seems I'm a little bit screwed, guess I'll just stick to the submit validation or try a third party validation script.
Many many thanks for your goodwill!
healyhatman
It worked for me
Web developer at SkySpider.com.au
I don't work for ChronoEngine but I do accept donations paypal.me/healyhatman
healyhatman
Post your current code, including the code you're using the bind the event and your event code.
Web developer at SkySpider.com.au
I don't work for ChronoEngine but I do accept donations paypal.me/healyhatman
C
Crams
My wrong! it works, just needed a slightly different logic approach, damn this was exhausting! lol...
Again many many thanks!
working code:
jQuery.fn.form.settings.rules.fmnem = function(value){​
​ var clientx = jQuery("select#client option:checked").val();​
​ var res;​
​ jQuery.ajax({​
​ url: '{url:search}&tvout=view',​
​ data: {client:clientx, nem:value},​
​ async:false,​
​ success: function(result){​
​ if(result == "empty"){​
​ res = true;​
​ }else{​
​ res = false;​
​ }​
​ },​
​ fail: function(result){​
​ res = false;​
​ }​
​ });​
​ return res;​
​};