Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为jQuery验证插件使用自定义方法以及如何匹配字符串值_Jquery_Spring Mvc_Jquery Validate - Fatal编程技术网

如何为jQuery验证插件使用自定义方法以及如何匹配字符串值

如何为jQuery验证插件使用自定义方法以及如何匹配字符串值,jquery,spring-mvc,jquery-validate,Jquery,Spring Mvc,Jquery Validate,我正在使用jquery.validate.js进行验证,以识别重复的条目 这是我的自定义方法 jQuery.validator.addMethod("uniqueName", function(name, element) { var response; $.ajax({ type: "POST", dataType : "json", url:"${pageContext.request.contextPath}/company/

我正在使用jquery.validate.js进行验证,以识别重复的条目

这是我的自定义方法

jQuery.validator.addMethod("uniqueName", function(name, element) {
    var response;
    $.ajax({
        type: "POST",
        dataType : "json",
        url:"${pageContext.request.contextPath}/company/getDuplicate",
        data:{"name":name},
        async:false,
        success:function(data){
            response = ( data == 'present' ) ? true : false;
        }
    })
     return response; 
}, "Name is Already Taken");
这是我在控制器中的Spring方法

@RequestMapping(value = "/company/getDuplicate", method = RequestMethod.POST, headers = "Accept=*/*")
    public @ResponseBody void getTitleList(@RequestParam(value="name") String name,HttpServletRequest request, HttpServletResponse response) {

       JSONObject json = new JSONObject();
       List<Company> matched = companyService.getDuplicate(name);

       System.out.println("Matched =====> "+matched);

       String s = "[]";

       if(s.equals(matched)){
           System.out.println(" Not Present");
           json.put("name", "notPresent");
           System.out.flush();
       }
       else{
           System.out.println("Present");
           json.put("name", "present");
           System.out.flush();
       }
   }
@RequestMapping(value=“/company/getDuplicate”,method=RequestMethod.POST,headers=“Accept=*/*”)
public@ResponseBody void getTitleList(@RequestParam(value=“name”)字符串名,HttpServletRequest请求,HttpServletResponse响应){
JSONObject json=新的JSONObject();
列表匹配=companyService.getDuplicate(名称);
System.out.println(“匹配====>”+匹配);
字符串s=“[]”;
如果(s.等于(匹配)){
系统输出打印项次(“不存在”);
json.put(“名称”、“不存在”);
System.out.flush();
}
否则{
系统输出打印项次(“当前”);
json.put(“名称”、“当前”);
System.out.flush();
}
}
通过这种方法,我可以获得数据是否重复(通过“匹配”变量),若数据库中存在数据,则返回相同的数据,若数据不存在,则返回“[]” (因为我使用了列表类型)

我的问题是:在if语句条件错误的情况下,对于所有数据,即使它们没有数据(即“matched”变量返回“[]”),它也会转到else块。以及如何在自定义验证方法中设置该状态

提前谢谢

 String s = "[]";    
 if(s.equals(matched))
这不是检查列表是否为空的方法,而是使用带有NOTNULL检查的
matched.isEmpty()
。因此您的条件将是

   if(matched!=null && matched.isEmpty()){
       System.out.println(" Not Present");
       json.put("name", "notPresent");
       System.out.flush();
   }
   else{
       System.out.println("Present");
       json.put("name", "present");
       System.out.flush();
   }

替换ajax方法

$.ajax({
            type: "POST",
            dataType : "json",
            url:"${pageContext.request.contextPath}/company/getDuplicate",
            data:{"name":name},
            async:false,
            success:function(data){
                /* alert(data); */
                response = ( data == true ) ? true : false;
            }
        })
         return response;
    }, "Name is Already Taken");

谢谢,它很好用。你能说一下如何在验证中使用它(请参见上文)至少给出建议。有效性?你是说在jquery.validation.js中?使用data.name而不仅仅是data。