0

My requirement is like to check whether the app name is already used or not using Ajax. I achieved those things , I planned to add those things in jquery validation. I added that using add method but if the response is false it shows the message

app name exists

error message. And also if the response is true it shows the error message as well.

Here's my code:

$(document).ready(function ()
          {
              function isAppNameExists() {
                  document.getElementById('imgLoad').style.display = "inline-table";
                  var appName =$("#txtAppName").val();//document.getElementById("txtAppName").value;

                    var tenantID =1;//document.getElementById("txttenantId").value;
                    if(window.XMLHttpRequest){
                        xmlhttp=new XMLHttpRequest();
                    } else {
                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    xmlhttp.onreadystatechange=function() {
                        if(xmlhttp.readyState==4 && xmlhttp.status==200){
                            if(xmlhttp.responseText == "true"){                 // App name already used 
                                    document.getElementById('imgLoad').style.display = "none";   
                                 return false;
                            } else {
                                document.getElementById('imgLoad').style.display = "none";   
                                return true;
                        }
                      }
                    }
                    xmlhttp.open("GET","ApplicationController?appNameCheck=createApp&appName="+appName+"&tenantId="+tenantID+"",true);
                    xmlhttp.send();
                  }


            $.validator.addMethod("appNameExistsValidation", function() {
                    return isAppNameExists();
            }, "Application name already exists");



              $('#storeAppCreation').validate( 
              {
                  rules:
                  {
                         appNameExistsValidation:true

                  }
             }

});
4

1 回答 1

1

您可以使用验证框架提供的远程选项

$(document).ready(function() {
    var tenantID =1;//document.getElementById("txttenantId").value;
    $('#storeAppCreation').validate({
        rules : {
            txtAppName: {
                remote : {
                    url : "ApplicationController",
                    type : "GET",
                    data : {
                        appNameCheck : 'createApp',
                        appName : function() {
                            return $("#txtAppName").val()
                        },
                        tenantId : tenantID
                    }
                }
            }

        }
    })

});
于 2013-04-29T05:09:34.193 回答