1

I have made this function, that seems to work fine, but it return "#" insted of wainting for the AJAX. I have tried to remove the return call, with out look.

It need to wait until the AJAX is finish before return. The JSP is working fine.

What is wrong?

jQuery.validator.addMethod("knidExist", function(value, element) {
    var valid = "#";
      $.ajax({
          async: false,
          type: "POST",
          url: "USER.jsp",
          data: "knid="+value,
          dataType: "html",
          success: function(msg) {
              // if the user exists, it returns a string "true"
              if($.trim(msg) != "true") {
                 //return false;
                 valid = false;  // already exists
              } else {
                 //return true;
                 valid = true;      // username is free to use
              }
          }  
     });
    return valid;
}, 'This USER does not exist');
7
  • The first A in AJAX stands for Asynchronous. Trying to make the function return after the call has finished is a very bad idea. You need to re-examine your approach instead. Commented Jan 13, 2012 at 15:04
  • I'm thrilled which solution somebody will provide for us ! Commented Jan 13, 2012 at 15:08
  • @Jon - they have set async: false. They are trying to do the call synchronously. Commented Jan 13, 2012 at 15:14
  • @jfriend00: It still remains a very bad idea. :) Commented Jan 13, 2012 at 15:15
  • @Jon - agreed. Synchronous ajax is evil. I would never use it myself. Locks up the browser. Your answer that allows use of async requests is probably a better way to go. Still, from a pure intellectual curiosity point of view, it isn't clear to me why what the OP has doesn't work. Commented Jan 13, 2012 at 15:18

3 Answers 3

6

Instead of adding your own validation method, you need to use the built-in remote validation method instead which is designed exactly for this purpose.

You would use it in a manner like

remote: {
    type: "POST", 
    url: "USER.jsp",  
    data: { knid: value },
}

Check out the examples the docs link to.

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

1 Comment

I know this method, but i am going to use this validation method 100 times, so it will be good to make it as a method as the jquery validation also can do.
4

I found a method by my self. Almost a complete copy from the remote method but with static message and url. Hope i can help some one.

jQuery.validator.addMethod("userExist", function(value, element, param) {
if ( this.optional(element) )
    return "dependency-mismatch";

var previous = this.previousValue(element);
if (!this.settings.messages[element.name] )
    this.settings.messages[element.name] = {};
previous.originalMessage = this.settings.messages[element.name].remote;
this.settings.messages[element.name].remote = previous.message;

param = typeof param == "string" && {url:param} || param;

if ( this.pending[element.name] ) {
    return "pending";
}
if ( previous.old === value ) {
    return previous.valid;
}

previous.old = value;
var validator = this;
this.startRequest(element);
var data = {};
data["**user**"] = value;
$.ajax($.extend(true, {
    url: "validation.jsp",
    mode: "abort",
    dataType: "json",
    data: data,
    success: function(response) {
        validator.settings.messages[element.name].remote = "**This user do not exist"**;
        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);
    }
}, param));
return "pending";
}, "");

Comments

0

Making those request types of "Post" type causes the issue. this basically submits the value and did not wait for the response. -try making it of type "get" it will work.

Comments

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.