开发者

need some help using bassistance validate and json to check username

I'm currently using the jquery bassistance validator on my form. I was trying to add a new function which will check (using json) whether a username already exists. I don't think I'm going the right way about it. Here's my code:

  jQuery.metadata.setType("attr", "validate");
  jQuery.validator.addMethod("uniqueUsername", function(value) {
    jQuery.getJSON("/index.php?option=com_json&format=raw", { task: "checkUsername", username: value }, function(data) {
      var response = eval(data);
      if (response == 1) {
        //does exist
      } else {
        //does not exist
      };
      alert(response);
    });
  }, "This username is currently开发者_运维知识库 unavailable.");
  jQuery("#adminForm").validate();

It currently checks immediately and alerts my response before I finish typing my username. It also gives the error regardless on which response is returned from json.

If someone has done this, or knows how to use a json check with bassistance - any help would be appreciated :)


That call to the server is asynchronous. So your function returns immediately to the validation plug-in.

Try checking a flag in the validation function (such as isUserNameValid). Then attach the ajax call to the 'change' event to do the validation and set the flag to the relevant value. Immediately set the flag to false and then do the ajax call.

HTH


The validation plugin has a remote method built in for this sort of thing. I would recommend you use that and return true/false from the method it calls.

See the following documentation:Remote Validation Documentation

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜