Custom Validation

Crams 07 Aug, 2018
1 Likes
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!!! 😲😲😲
Crams 07 Aug, 2018
Any ideas? Anyone?
healyhatman 09 Aug, 2018
Answer
1 Likes
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
};
Crams 11 Aug, 2018
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 11 Aug, 2018
Post your current code, including the code you're using the bind the event and your event code.
Crams 11 Aug, 2018
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;
};
This topic is locked and no more replies can be posted.