function name_free(value, element){	//validation rule
	var result;
	if($('input[name="name"]').val() != value)
	{
		//console.log($('input[name="name"]').val());
		$.post(CI.base_url + "admin_/info_psl/name_check_ajax", { pavadinimas: value}, function(data){	
			//console.log(data);
			if(data == 'true')
				result = true;
			else result = false;
		 });
	}
	else result = true;	//jei vienodi tai viskas ok
	console.log(result);
	return result;
}

when
if($('input[name="name"]').val() != value)

is true, then result variable is not set, it is undefined. How to assign a value for it in post callback funtion?

There's no point trying to return result from the outer function. It returns BEFORE the AJAX success funtion. Reason ... AJAX is ASYNCHRONOUS!

You need to do whatever you need to do with result in the inner function(data){...}.

function name_free(value, element){	//validation rule
	if($('input[name="name"]').val() != value)
	{
		$.post(CI.base_url + "admin_/info_psl/name_check_ajax", { pavadinimas: value}, function(data){	
			if(data == 'true') {
				//DO SOMETHING HERE;
			}
			else {
				//DO SOMETHING ELSE HERE;
			}
		 });
	}
}

Airshow

Ok, I understand now. So I made different solution because I have no luck with those returns.

$('input.save').click(function() {
        
        $('form').attr( 'target', '' );
        
        valid = $(".edit").validate({		
            
            rules: {
                 input_pavadinimas: {
                  required: true,
                  maxlength: 20,
                  //name_free: true	//custom metodas
                  remote: {
                      url: CI.base_url + "admin_/info_psl/name_check_ajax",
                      type: "post",
                      data: {
                        pavadinimas: function() {
                          return $("input[name='input_pavadinimas'").val();
                        },
        		current_name: function(){
                        	return $('input[name="name"]').val()
                        }
        			  }
                  }
        		}, 
                text: {
                    required: true
                }
                
            },
            messages: {
                input_pavadinimas:{
                    required: "Neįvedėte pavadinimo",
                    maxlength: "Maksimalus ilgis - 20 simbolių"
                },    
                
                text: {
                    required: "Neįvedėte teksto"
                }
            }
            
        }
                
        
        ).form();
        //alert('');
        if(valid)
        	alert('valid');
        else alert('not valid');
   //     	document.myform.submit();
    });

But the problem is: sometimes I get alert “valid”, when I enter invalid data, but also it shows an error near the field. Have you idea how can that be?

I would need to consult the appropriate API for the jQuery plugin you are using.

Airshow

Made a short version:

<scri+pt src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" type="text/javascript"></scri+pt>
<s+cript  src="http://www.ylakiudarzelis.lt/js/jquery.validate.js" type="text/javascript"></s+cript>
<scr+ipt>
$(function(){
$('input.save').click(function() {
        
        $('form').attr( 'target', '' );
        
        valid = $(".edit").validate({        
            
            rules: {
                 input_pavadinimas: {
                  required: true,
                  maxlength: 20,
                 
                remote:  "http://www.ylakiudarzelis.lt/admin_/info_psl/name_check_ajax"
                }                
            }
        
            
        }
                
        
        ).form();
        //alert('');
        if(valid)
            alert('valid');
        else alert('not valid');
            //document.myform.submit();
    }); 

    
});

</scr+ipt>

&lt;form action="http://www.ylakiudarzelis.lt/test/valid" method="post" class="wide edit" name="myform"&gt; 
    
     
        <br /><label for="label">Puslapio pavadinimas:*</label> 
&lt;input type="hidden" name="name" value="asdasd" /&gt; 
&lt;input type="text" name="input_pavadinimas" value="asdasd" maxlength="20" size="10" style="width:20%"  /&gt;&lt;br /><br />        
         <br> 
         
        &lt;input class = "save" type="button" value="Išsaugoti" /&gt; 
     
    
&lt;/form&gt;

php validation always echoes false

function name_check_ajax()
    {
        echo 'false';
    }

When I run this code, I get an alert "valid" and after alert the message appeards near input field - "Please fix this field."

What do I need to change there so the variable "valid" could be set to false?

At line 27, try:

alert([typeof valid, valid, valid.length].join("\n"));//debug statement

What do you get?

Airshow

I think I found where is the problem. I made remote methoid like this:

remote: {
                    url: "http://www.ylakiudarzelis.lt/admin_/info_psl/name_check_ajax",
                    type: "post",
                    data: {
                        pavadinimas: function() {
                          return $("input[name='input_pavadinimas']").val();
                        },
        				current_name: function(){
                        	return $('input[name="name"]').val()
                        }
        			  },
                    async:false, //this line mainly solves the problem
                    
                	}

The problem was when it acted asynchronyously, the the requust was not finished and was just ignored. Still don't understand why the plugin creators didn't made async:false by default. What is the point of checking if valid and ignoring the result?

I think I found where is the problem. I made remote methoid like this:

remote: {
                    url: "http://www.ylakiudarzelis.lt/admin_/info_psl/name_check_ajax",
                    type: "post",
                    data: {
                        pavadinimas: function() {
                          return $("input[name='input_pavadinimas']").val();
                        },
        				current_name: function(){
                        	return $('input[name="name"]').val()
                        }
        			  },
                    async:false, //this line mainly solves the problem
                    
                	}

The problem was when it acted asynchronyously, the the requust was not finished and was just ignored. Still don't understand why the plugin creators didn't made async:false by default. What is the point of checking if valid and ignoring the result?

Thanks for the responses :)

SPeed_FANat1c,

Agree completely.

I looked in the plugin's API and didn't see any obvious warning about that. The described syntax, valid = .... kind of implies that it should be synchronous without nededing to make a setting.

Be sure to test in various browsers. Most don't handle synchronous "AJAX", at least not when performed in the standard way. IIRC, IE is OK but FF (and other MOZ derivitives) are not. With a bit of luck the plugin developers have included some sort of workaround.

Bon chance

Airshow

I made it to work somehow without need async:false on the test page, then copy pasted to the project page and it did't work. But then I found that I can do the validation witout ajax, just by loading all taken names into the javascript variable when loading the page :) so the problem is solved :)

SPeed_FANat1c,

The only thing about a client-side solution is that it reveals all taken names to the general public. ("view source" and all is revealed).

A server-side solution would keep them private.

Personally, I would persist with the server-side approach but with standard jQuery .ajax() rather than the validator plugin, which seems a bit awkward.

Airshow

No problem there - I check if menu items are taken, peaople are seeing menu items in website :)

:cool:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.