2010-04-13 11 views
43

का उपयोग करता है, मैंने jQuery सत्यापनकर्ता के बारे में आपका उत्तर पढ़ा है जहां आप डेटाबेस में किसी मान के विरुद्ध उपयोगकर्ता नाम की जांच करने के लिए एक विधि की रूपरेखा तैयार करते हैं।jQuery सत्यापनकर्ता और एक कस्टम नियम जो AJAX

Ive ने इस विधि को लागू करने का प्रयास किया लेकिन कोई फर्क नहीं पड़ता कि PHP फ़ाइल से क्या लौटाया गया है, मुझे हमेशा यह संदेश मिलता है कि उपयोगकर्ता नाम पहले ही लिया जा चुका है।

यहाँ यहाँ चौथाई कस्टम विधि है ...

$.validator.addMethod("uniqueUserName", function(value, element) { 
    $.ajax({ 
     type: "POST", 
     url: "php/get_save_status.php", 
     data: "checkUsername="+value, 
     dataType:"html", 
    success: function(msg) 
    { 
     // if the user exists, it returns a string "true" 
     if(msg == "true") 
     return false; // already exists 
     return true;  // username is free to use 
    } 
})}, "Username is Already Taken"); 

और मान्य कोड है ...

username: { 
    required: true, 
    uniqueUserName: true 
}, 

वहाँ एक विशिष्ट तरीके से मैं PHP से संदेश वापस करना चाहिए है।

धन्यवाद

एक

उत्तर

57

आप एक AJAX अनुरोध कर रहे हैं, Ergo: सत्यापन पहले से ही, जब आपके कस्टम सत्यापनकर्ता रिटर्न सही या गलत काम कर समाप्त हो गया है।

आपको async के साथ काम करने की आवश्यकता होगी। यह भी देखें इस पोस्ट: How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?

कुछ की तरह:

function myValidator() { 
    var isSuccess = false; 

    $.ajax({ url: "", 
      data: {}, 
      async: false, 
      success: 
       function(msg) { isSuccess = msg === "true" ? true : false } 
      }); 
    return isSuccess; 
} 

चेतावनी:

As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done() or the deprecated jqXHR.success().

+0

धन्यवाद। आपके जवाब ने एक इलाज किया! – thatweblook

+0

@thatweblook कृपया आप अपना काम करने वाला नमूना कोड – prashu132

+0

दे सकते हैं इसे काम करने के लिए, मुझे msg === "सत्य" को === सत्य (फ़ायरफ़ॉक्स) में बदलना पड़ा। रास्ते के समाधान के लिए धन्यवाद! – ndemoreau

54

के लिए, जो इस पर ठोकर किसी और को, मान्य समर्थन करता है 'दूरस्थ' विधि, नहीं हो सकता है जो 2010 में अस्तित्व में:

http://docs.jquery.com/Plugins/Validation/Methods/remote

$("#myform").validate({ 
    rules: { 
    email: { 
     required: true, 
     email: true, 
     remote: { 
     url: "check-email.php", 
     type: "post", 
     data: { 
      username: function() { 
      return $("#username").val(); 
      } 
     } 
     } 
    } 
    } 
}); 
+0

क्या यह एसिंक अनुरोध या सिंक अनुरोध कर रहा है? – Raghav

+0

@ राघव, यह एक असीमित सत्यापन – ekkis

+0

है क्या आप कृपया मुझे यह बता सकते हैं कि इस दूरस्थ अनुरोध में देरी कैसे करें? आपके कथन के संबंध में – Jaswinder

7

यह मुझे यह जानने के लिए हमेशा के लिए ले गया कि पेज में किसी तत्व के मूल्य को दूरस्थ अनुरोध में कैसे जोड़ा गया है- यह कई घंटों के संयोजन का परिणाम है और कई खोज परिणामों का प्रयास कर रहा है।

मुख्य बिंदु:

  1. async:false पदावनत किया गया है,
  2. समारोह कॉल सही remote: के बाद तत्व का मान के साथ डेटा स्ट्रिंग बनाने के लिए महत्वपूर्ण है। data: के बाद फ़ॉर्म से वर्तमान मान तक पहुंचने का प्रयास करने से dataType जेसन के रूप में सेट के साथ फ़ील्ड के लिए रिक्त मान लौटा रहा था।

    $("#EmailAddress").rules("add", { 
        required: true, 
        remote: function() { // the function allows the creation of the data string 
              // outside of the remote call itself, which would not 
              // return a current value from the form. 
         var emailData = "{'address':'" + 
             $('#SignupForm :input[id$="EmailAddress"]').val() + 
             "'}"; 
         var r = { 
          url: "foobar.aspx/IsEmailAvailable", 
          type: "post", 
          dataType: "json", 
          contentType: "application/json; charset=utf-8", 
          cache: false, 
          data: emailData, 
          dataFilter: function(response) { 
           this.email_run = true; //fix for race condition with next button 
           return isAvailable(data); //return true or false 
          } 
         }; 
         return r; 
        }, 
        messages: { 
         remote: "* Email in use" 
        } 
    }); 
    

।ASPX पेज:

<input id="EmailAddress" required name="Email" type="email" placeholder="Email Address*" runat="server"/> 

सी # के पीछे कोड:

[WebMethod] 
    public static object IsEmailAvailable(string address){...} 

प्रतिक्रिया ऑब्जेक्ट प्रारूपण:

function isAvailable(data) { 
    var response = JSON.parse(getJsonObject(data)); 
    return (response.isAvailable === "True") ? true : false; 
}; 

//transform response string to a JavaScript Object() 
//http://encosia.com/never-worry-about-asp-net-ajaxs-d-again/ 
function getJsonObject(data) { 
    var msg = eval('(' + data + ')'); 
    if (msg.hasOwnProperty('d')) 
     return msg.d; 
    else 
     return msg; 
}; 
+0

- '1। async: झूठी बहिष्कृत कर दी गई है, '.. हाँ, लेकिन यह अभी भी अनुमति है बशर्ते आप उचित कॉलबैक/हैंडलर (जैसे' पूर्ण: ', 'सफलता:',' त्रुटि: ') का उपयोग वादा विधि जैसे' .then' या '.done' ... यहां निम्न संदर्भ देखें ==> http://bugs.jquery.com/ticket/11013#comment:40 – CodeBurner

संबंधित मुद्दे