2

I am using javascript in client side and node.js in server side, Actually i have tried to make a remote validation for input fields, result must be boolean and it should be based on the server call, My code as follows,

 jQuery.validator.addMethod("remoteValidation", function(value, element) {

    var accountName = $("#accountName").val();
    var accountType = $("#accountType").val();
    var valid = this.Validation(accountType,accountName);
   //returning undefined becoz return statement executing before server side execution
    return valid;

}, "Entered email or username already exists");

 this.validation = function(accountType,accountName,cb){
    var valid =null;
  ss.rpc('AccountsManagement.userNameChecker',accountType,accountName,function(res) {
            // res will return boolean
        valid = res;
    });
            //valid is null becoz this line is executing before valid = res line.
    return valid;
};

my server side code:

   services.imAccountsService.getAllImAccounts(accountType,accountName,
   function(err,result){
    if(err){
    return;
    }
    if(result == null){
       res(true);
    }
    else{
       res(false);
    }

   });

My problem is async execution, how to make code synschronous in this situation..

3
  • What is ss? If there is documentation for that method you should consider looking there as it is the asynchronous method. Commented Nov 5, 2012 at 7:01
  • @Quintin ss.rpc is an asynchronous method. it is node js Commented Nov 5, 2012 at 7:07
  • Is socketstream included in the default node.js package? I wasn't aware of that if it is. Commented Nov 5, 2012 at 7:12

2 Answers 2

1

Added function in the jquery validation method.

   if(param.type === "rpc"){
            var args = param.data.remoteArgs();
            ss.rpc(param.url, args, function(response) {
                validator.settings.messages[element.name].remote = previous.originalMessage;
                var valid = response === true;
                if ( valid ) {
                    var submitted = validator.formSubmitted;
                    validator.prepareElement(element);
                    validator.formSubmitted = submitted;
                    validator.successList.push(element);
                    validator.showErrors();
                } else {
                    var errors = {};
                    var message = response || validator.defaultMessage( element, "remote" );
                    errors[element.name] = previous.message = $.isFunction(message) ? message(value) : message;
                    validator.showErrors(errors);
                }
                previous.valid = valid;
                validator.stopRequest(element, valid);
            });
        }

It works fine ....thanks for all...

Sign up to request clarification or add additional context in comments.

Comments

0

You will have to wait till the value of res is available. See if the below code works.

    jQuery.validator.addMethod("remoteValidation", function(value, element) {

        var accountName = $("#accountName").val();
        var accountType = $("#accountType").val();
        var valid = this.Validation(accountType,accountName);
       //returning undefined becoz return statement executing before server side execution
        return valid;

    }, "Entered email or username already exists");

     this.validation = function(accountType,accountName,cb){
        var valid =null;
      ss.rpc('AccountsManagement.userNameChecker',accountType,accountName,function(res) {
                // res will return boolean

            while(res <> null)
            {
               window.setInterval(function(){waitFunc()}, 1000);
            }
            valid = res;
        });
                //valid is null becoz this line is executing before valid = res line.
        return valid;
    };

function waitFunc()
{
//do nothing
}

6 Comments

thanks , but ss.rpc('AccountsManagement.userNameChecker',accountType,accountName,function(res) itself asynchronous code..
thats right.. but when it executes, res will have some value, isn't that what you expect?
yes you are right but , the ss.rpc callback is not running befre that return gets executed,if i remove return ss.rpc callback is running i have added server side code , plz look at that...
yes. It will give you true/false. Did you try adding the code given by me? what result did you get? Does the while loop exit correctly or goes into infinite loop?
This won't work. The while loop will go into an infinite loop and time out. This is because javascript, the networking/IO code and the HTML/DOM parser takes turns in a browser to execute. As long as the javascript code doesn't end, the networking code doesn't send out the rpc request.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.