2013-04-29 59 views
0

我的需求就像是检查应用程序名称是否已被使用或不使用Ajax。我实现了这些目标,我计划在jQuery验证中添加这些内容。我添加,使用添加的方法,但如果响应是false它显示在jQuery验证插件的添加方法中调用函数

应用名称存在

错误消息的消息。并且如果响应是true,它也会显示错误消息。

这里是我的代码:

$(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 

        } 
      } 

}); 

回答

1

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

$(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 
        } 
       } 
      } 

     } 
    }) 

}); 
相关问题