2012-02-12 8 views
0

아약스가있는 필드와 그 값이 이미 기지가 아닌지 확인하는 함수의 유효성 검사를 시도합니다.jquery 유효성 검사 엔진 : ajax 필드 유효성 검사 (PHP가 아닌)

내가 발견 한 모든 사례는 PHP로 제공됩니다. 자바와 함께하는 방법?

JSP 코드 :

<form id="FLoginNew" action="Loginnew" method="post"> 
..... 
<script> 
$(document).ready(function(e) { 
    $("#FLoginNew").validationEngine('attach', {promptPosition : "centerRight", scroll: false, validationEventTrigger: "blur", autoHidePrompt:true, autoHideDelay:3000 }); 
}); 
</script> 

jquery.ValidationEngine-fr.js은 변경 :

"ajaxLoginCall": { 
       "url": "myapp.selvlets.DoublonLoginServlet", 
       "extraDataDynamic": ['#login'], 
       "alertText": "* Ce login est déjà pris", 
       "alertTextOk": "* Ce login est disponible", 
       "alertTextLoad": "* Chargement, veuillez attendre" 
      }, 

는 URL, 나는 시도 : "url": "/DoublonLogin",를 서블릿 매핑의 선언이다 web.xml.

답변

0

나 자신에 대답 해요 : 처음에는

, 입력 클래스 (I가 표시되지 않았습니다) : class="validate[required,ajax[Ajaxdoublonlogin]]"

그런 다음, "Ajaxdoublonlogin"라는 Ajax 호출의 URL 나는 해결책을 발견 : "Doublonlogin"은 web.xml 파일에 선언되어 있습니다. 그것은 서블릿입니다.

서블릿 :

  • 는 그것은 JSON의 librairy를 가져와야합니다. 나는 "JSONSimple"을 간단하게 선택했고, 내가하고 싶은 것을 충분히 얻을 수있는 을 선택했다.
  • 확인 후 JSON 배열을 만들고 입력 필드 이름을 true 또는 false 으로 지정합니다.
  • 응답 개체에 작성기를 작성하고 JSON 배열을 작성하십시오.

어렵지 않습니다!^_^

예 : 입력 필드의 ID가

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

    String login = new String(request.getParameter("login")); // we catch the value 

    JSONArray jsres = new JSONArray(); // new JSON array 

    jsres.add("login"); // 1st field : the field name = "login" 

    if(<login in the database ?>) jsres.add(new Boolean(false)); // In database ? => false in the JSON array 
    else jsres.add(new Boolean(true)); // Not in database > true in the JSON array !! 

    response.setContentType("application/json"); // The answer is in JSON content type 
    response.setCharacterEncoding("UTF-8"); // Put the right encoding 
    PrintWriter writer = response.getWriter(); // new answer writer 

    try { 
     writer.println(jsres); // WRITE !! ^_^ 
     log.info("Retour JSON : " + jsres); 
     } catch(Exception e){ 
     log.error("Erreur lors du transfert JSON : " + e.getMessage()); 
    } finally { 
     writer.close(); // Don't forget to close the writer 
    } 
} 

그것은 잘 작동 "로그인"당신이 제안이 있다면 ... , 나는 즐거움과 함께 할게요!

이제 마지막 조건이이 조건 인 경우 제출이 추가되므로 양식 제출을 막으려 고합니다. 이유를 모르겠다.

관련 문제