تمديد مسج المصادقة المساعد مع أسلوب مخصصة
-
06-07-2019 - |
سؤال
وأضفت طريقة التحقق من صحة مخصصة للتحقق من صحة كلمة المرور. ومع ذلك، فإنه لا يهم إذا كان JSON أحصل هو:
{"success":true}
وأو:
{"success":false}
والحقل كلمة المرور م> لا يصدق.
$(document).ready(function() {
// Ad custom validation
$.validator.addMethod('authenticate', function (value) {
$.getJSON("./json/authenticate.do",{ password: value},function(json) {
return (json.success == true) ? true : false;}
);
}, 'Wrong password');
$('form#changePasswordForm').validate({
rules: {
repeat_new_password: { equalTo: "#new_password" },
password : {authenticate: true}
}, submitHandler: function(form) {
$(form).ajaxSubmit( {
dataType: "json",
success: function(json) {
alert("foo");
}
});
}
});
});
وأي فكرة، ما أقوم به خطأ؟
المحلول
ما عليك فعله هو أن الخطأ عند إضافة طريقة مخصصة لك أبدا العودة صحيحة أو خاطئة من ذلك. إعادته في رد اياكس.
$.validator.addMethod('authenticate', function (value) {
$.getJSON("./json/authenticate.do",{ password: value }, function(json) {
// This return here is useless
return (json.success == true) ? true : false;
});
// You need to return true or false here...
// You could use a synchronous server call instead of asynchronous
}, 'Wrong password');
وبدلا من إضافة أسلوب مخصصة يمكنك استخدام وظيفة عن بعد :
$('form#changePasswordForm').validate({
rules: {
repeat_new_password: {
equalTo: "#new_password"
},
password : {
// This will invoke ./json/authenticate.do?password=THEVALUE_OF_THE_FIELD
// and all you need to do is return "true" or "false" from this server script
remote: './json/authenticate.do'
}
},
messages: {
password: {
remote: jQuery.format("Wrong password")
}
},
submitHandler: function(form) {
$(form).ajaxSubmit({
dataType: "json",
success: function(json) {
alert("foo");
}
});
}
});
ويمكنك التحقق من ذلك في العمل هنا .
لا تنتمي إلى StackOverflow