2009-10-14 6 views
15

jquery 유효성 검사를 사용하고 있으며 이메일 유효성을 검사해야합니다.ajax 호출로 jquery 확인

는 지금까지 너무 좋아

$("#myForm").validate({ 
     rules: 
      email: { 
       required: true, 
       email: true 
      } 
}) 

를 사용합니다. 문제는 주어진 이메일이 이미 존재하는지 확인하기 위해 아약스 호출을해야한다는 것입니다. 표시 메시지 "이 이메일은 이미 종료되었습니다. 다른 메일을 선택하십시오"가 표시됩니다.

누구든지 구현할 수 있습니까?

답변

16
remote: "/some/remote/path" 

해당 경로는 $ _GET의 필드 값을 전달합니다. 때문에 .. 실제로 경우에 호출되는 것 같습니다

/some/remote/path?email=someemailuriencoded 

은 서버 측 코드를 반환 단지 텍스트 참 또는 거짓을 가지고.

그러면 해당 메시지의 이름이 remote입니다. 비슷한에 대한

remote: "The corresponding email already exists" 

내 코드 : PHP에서

$("#password_reset").validate({ 
    rules: { 
    email: { required: true, email: true, minlength: 6, remote: "/ajax/password/check_email" } 
    }, 
    messages: { 
    email: { required: "Please enter a valid email address", minlength: "Please enter a valid email address", email: "Please enter a valid email address", remote: "This email is not registered" } 
    }, 
    onkeyup: false, 
    onblur: true 
}); 

대응하는 서버 측 코드 :

$email_exists = $db->prows('SELECT user_id FROM users WHERE email = ? LIMIT 1', 's' , $_GET['email']); 
if ($email_exists) { echo 'true'; } else { echo 'false'; } 
exit; 
내 데이터베이스 추상화 재료를 사용하고 물론

,하지만 당신은 그것을 얻을.

+0

은 다렌 감사합니다 ID로 입력이

$('[id$=txtEmail]').rules("add", { required: true, email: true, remote:function(){ var checkit={ type: "POST", url: WebServicePathComplete+"VerifyEmail", contentType: "application/json; charset=utf-8", dataType: "json", data: "{'email':'" +$('[id$=txtEmail]').val() + "'}" }; return checkit; } }); 

메모를 작동 . 나는 그것을 시험 할 것이다, 나는 thats 다라고 생각할 것이다 –

+0

나는 단지 리모콘을위한 메시지를 어떻게 추가 할 수 있냐. .. 또는 내가 할 수있는 무엇인가 'onsuccess – jack

+0

당신은 단지 시간의 시간을 절약했다. 감사합니다 – Sino

0

서버 언어는 무엇입니까? PHP 또는 ASP?

이것은 jQuery를 부분입니다 :

$.ajax({ 
    type: "POST", 
    url: "YourWebserviceOrWhatEver", 
    data: "{'Email':'[email protected]'}", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function(msg) { 
     if(msg.EmailExists){ 
     //Email exists 
     alert("This email already exists. Please select other"); 
     } 
     else { 
     //Email doesn't exist yet 
     doSomething(); 
     } 
    } 
}); 
+0

그는 ajax가 내장 된 validate를 사용하고 있습니다. –

+0

저자가 jQuery 유효성 검사 플러그인 (http://docs.jquery.com/Plugins/Validation)의 사용을 의미한다고 생각한다고 생각하십시오. 아약스 전화가 아닙니다. – Kamarey

+0

아, 그 사실을 알지 못했습니다. – k0ni

7

음,이 ... 나를 위해 내가 'txtMail'

관련 문제