0

Basically, I need to test if the sub-domain exists or not so I added a method uniqueSubdomain to my jQuery Validate rules:

$.validator.addMethod("uniqueSubdomain", function(value, element) {
 $.ajax({
  type: "POST",
   url: "ajax.php",
   data: 'subdomain='+ value,
   cache: false,
success: function(msg)
{
alert(msg);
  // if the subdomain exists, it returns a string "true"
  if(msg == "true"){
    return false;  // already exists
  }else{
    return true;    // subdomain is free to use
  }      
 }
 })}, "sub-domain already exists!");

And in the rules :

subdomain: {               
            required: true,
            uniqueSubdomain: true           
        },

But it seem that it only displays sub-domain already exists! Even if it doesn't exist! Any help with this, Thanks!

5
  • try '&subdomain='+value Commented Apr 9, 2014 at 10:55
  • or data: {subdomain: value}, Commented Apr 9, 2014 at 10:58
  • Sorry but I don't think that extracting the value or the issue here since the AJAX request respond with false and it still the same !! Commented Apr 9, 2014 at 11:02
  • Your ajax req is true, You need to check your php service. Have you checked network tab for ajax response? You said it is not exists but what is the response from ajax? I think your ajax response is always returns true. Post your php code to your question Commented Apr 9, 2014 at 11:02
  • When a type an non-exist sub-domain my ajax req response comes as false & alert(msg) (means it doesn't exist) but it still the same display "sub-domain already exists!" !! Commented Apr 9, 2014 at 11:12

4 Answers 4

1

You are using an AJAX request to retrieve a result for your validation, BUT: the validation may have finished when the ajax returns a result becuase you are dealing with an asynchrounous call.

You need to make your ajax request synchrounous so the validation won't process utill a result is returned with async: false.

Something like:

$.validator.addMethod("uniqueSubdomain", function(value, element) {
  $.ajax({
    type: "POST",
    url: "ajax.php",
    data: 'subdomain='+ value,
    async: false,
    cache: false,
    success: function(msg)
    {
    alert(msg);
    // if the subdomain exists, it returns a string "true"
    if(msg == "true"){
      return false;  // already exists
    }else{
      return true;    // subdomain is free to use
    }      
  }
 })}, "sub-domain already exists!");

And since async option is deprecated you can solve it by performing a remote validation:

$( "#form" ).validate({
  rules: {
    subdomain: {
      required: true,
      uniqueSubdomain: true,
      remote: {
        url: "ajax.php",
        type: "post",
        data: 'subdomain='+ value
      }
    }
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

I totally agree with you but, I don't get it that when a type an non-exist sub-domain my ajax req response comes as false & alert(msg) (means it doesn't exist) but it still the same display "sub-domain already exists!" !!
1

Add the following line in false case

$("div.error").css({ display: "none" });

For your example

$.validator.addMethod("uniqueSubdomain", function(value, element) {
     $.ajax({
      type: "POST",
       url: "ajax.php",
       async: false,
       data: 'subdomain='+ value,
       cache: false,
    success: function(msg)
    {
    //alert(msg);
      // if the subdomain exists, it returns a string "true"
      if(msg == "true"){
        return false;  // already exists
      }else{
        $("div.error").css({ display: "none" });
        return true;    // subdomain is free to use
      }      
     }
    });
 }, "sub-domain already exists!");

FYI: I believe that your error container is "div", if not please change the below line as

$("errorContainer.error").css({ display: "none" });

Comments

1

Another perfect solution is, we need to return the flag for validation method.

$.validator.addMethod("uniqueSubdomain", function(value, element) {
    var isFlag;
    $.ajax({
      type: "POST",
       url: "ajax.php",
       async: false,
       data: 'subdomain='+ value,
       cache: false,
    success: function(msg)
    {
    //alert(msg);
      // if the subdomain exists, it returns a string "true"
      if(msg == "true"){
        isFlag =  false;  // already exists
      }else{
        //$("div.error").css({ display: "none" });
        isFlag = true;    // subdomain is free to use
      }      
     }
    });

    return isFlag;
 }, "sub-domain already exists!");

2 Comments

When I type a sub-domain that already exist it doesn't display anything, but as soon as I type an non-existed sub-domain it display Please fix this field.
I have checked it in FF, IE11, Chrome & Safari and its working fine. Could you please clear cache and check it again?
1

Your validation and ajax is working at parallel. When your validation done, ajax may not finish. For this, you need to use async:false. and you can use following(tested);

$.validator.addMethod("uniqueSubdomain", function(value, element) {
    var domainOk = false
     $.ajax({
      type: "POST",
       url: "ajax.php",
       data: 'subdomain='+ value,
       cache: false,
       success: function(msg) {
          // if the subdomain exists, it returns a string "true"
          if(msg == "true"){
            domainOk = false
          }else{
            domainOk = true;
          }      
         }
     });
     return domainOk;
 }, "sub-domain already exists!");

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.