• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

Jquery validation

Hello experts.
I'm trying to validate a user password ,adding a addmethod function to my script but i get an error:
ReferenceError: response is not defined
return response;

My function is returning a boolean value!

I don't want to use the build in remote function of the validation plugin.
Any help?
 $.validator.addMethod("isu<wbr ></wbr>ser", function (value, element) {
                $.ajax({
                    type: "POST",
                    url: "sourcetomyfunction",
                    data: "oldpword=" + value,
                    dataType: "json",
                    success: function (msg) {
						alert(msg);
                        //If username exists, set response to true
                        response = (msg == 'true') ? true : false;
                    }
                })
                return response;
            }, "Wrong password");
            $("#frmchgpassw").validate<wbr ></wbr>({
                rules: {
                    oldpword: {
                        required: true,
                        isuser: true
                    },
                    pword: {
                        required: true
                    },
                    PasswordConfirm: {
                        required: true
                    }
                },
                messages: {
                    oldpword: {
                        required: 'required'
                    },
                    pword: {
                        required: 'required'
                    },
                    PasswordConfirm: {
                        required: 'required',
                        equalTo: "Fields not equal"
                    }
                }
            });
        

Open in new window

0
Panos
Asked:
Panos
  • 5
  • 3
1 Solution
 
Chris StanyonCommented:
You can't return a value from an ajax call like that because Ajax is asynchronous. When you call you ajax function, it makes a call to the server. Whilst it's dealing with that call, your program has already continued to the end and tried to return the response - which it hasn't had yet! If you want a return value then your will have to set your request to synchronous which has the downside of blocking the browser. Something like this:

var response = false;

$.ajax({
   type: "POST",
   async: false, //make this synchronous
   url: "sourcetomyfunction",
   data: "oldpword=" + value,
   dataType: "json",
   success: function (msg) {
      alert(msg);
      response = (msg == 'true') ? true : false;
   }
});

//this now won't run until the ajax call has finished
return response;

Open in new window

0
 
cmalakarCommented:
Try declaring variable "response" before the line

$.validator.addMethod(....
0
 
PanosAuthor Commented:
Hi.
Thank you for your posts.
I'm not getting any error but       although the response is true i get the error message "Wrong password".
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
PanosAuthor Commented:
In detail:
The first alert is true and the second false:
$.validator.addMethod("isuser", function (value, element) {
                        var response = false;
                $.ajax({
                    type: "POST",
                              async: false, //make this synchronous
                    url: "com/resetemailpassword.cfc?method=fcheckpassword",
                    data: "oldpword=" + value,
                    dataType: "json",
                    success: function (msg) {
                           alert(msg);
                        //If username exists, set response to true
                        response = (msg == 'true') ? true : false;
                    }
                });alert(response);
                return response;
            }, "Wrong password");
            $("#frmchgpassw").validate({........
0
 
Chris StanyonCommented:
Unless you set your ajax call to be synchronous, alert(response) will always be called before your ajax script has had time to return from the server (before you set response).
0
 
PanosAuthor Commented:
hmmmmm...
What is the solution to make this work?
Is there any other way? Setting this to synchronous is not solving the problem.
0
 
Chris StanyonCommented:
If you want to make a call to your server in order to validate the info, then you will need to wait for the server response by setting the request to be synchronous. An asynchronous call just won't work, for the reasons I've already outlined.

In your code, you are requesting a dataType of JSON but then you are handling the response as if it's text ('true'). If you ask for JSON and don't get JSON back then your success function won't fire. Change the dataType to "text".

You have also assigned a weird name to your method. Is that just a typo (or result of copy/paste)?

Your addMethod code should look like this:

$.validator.addMethod("isuser", function (value, element) {
	var response = false;
	$.ajax({
		type: "POST",
		url: "com/resetemailpassword.cfc?method=fcheckpassword",
		data: "oldpword=" + value,
		dataType: "text",
		async: false,
		success: function (msg) {
			response = (msg == 'true') ? true : false;
		}
	});
	return response;
}, "Wrong password");

Open in new window

0
 
PanosAuthor Commented:
The big problem was that i didn't knew about te sync and async attribute.
I had this problem using the remote function that has async as true by default.
So i tested the remote function again having:remote: { url:'com/functions.cfc?method=fcheckpassword', async:false}

Now it is working perfect. Thank you for your help.
0
 
PanosAuthor Commented:
Thank you
regards
panos
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now