官方jquery.validationEngine.js 文件中的关键代码 $.ajax({ type: options.ajaxFormValidationMethod, url: rule.url, cache: false, dataType: "json", data: "fieldId=" + field.attr("id") + "&fieldValue=" + field.val() + "&extraData=" + extraData + "&" + extraDataDynamic, field: field, rule: rule, methods: methods, options: options, beforeSend: function() { // build the loading prompt var loadingText = rule.alertTextLoad; if (loadingText) methods._showPrompt(field, loadingText, "load", true, options); }, error: function(data, transport) { methods._ajaxError(data, transport); }, success: function(json) { // asynchronously called on success, data is the json answer from the server var errorFieldId = json[0]; //var errorField = $($("#" + errorFieldId)[0]); var errorField = $($("input[id='" + errorFieldId +"']")[0]); // make sure we found the element if (errorField.length == 1) { var status = json[1]; // read the optional msg from the server var msg = json[2]; if (!status) { // Houston we got a problem - display an red prompt options.ajaxValidCache[errorFieldId] = false; options.isError = true; // resolve the msg prompt if(msg) { if (options.allrules[msg]) { var txt = options.allrules[msg].alertText; if (txt) msg = txt; } } else msg = rule.alertText; methods._showPrompt(errorField, msg, "", true, options); } else { if (options.ajaxValidCache[errorFieldId] !== undefined) options.ajaxValidCache[errorFieldId] = true; // resolves the msg prompt if(msg) { if (options.allrules[msg]) { var txt = options.allrules[msg].alertTextOk; if (txt) msg = txt; } } else msg = rule.alertTextOk; // see if we should display a green prompt if (msg) methods._showPrompt(errorField, msg, "pass", true, options); else methods._closePrompt(errorField); } } errorField.trigger("jqv.field.result", [errorField, options.isError, msg]); } });
// --- CUSTOM RULES -- Those are specific to the demos, they can be removed or changed to your likings "ajaxUsers": { "url": "AjaxBackstage/AjaxValidation.ashx", // "validate.action", "validate.action"ajax验证用户名,会post如下参数:validateError ajaxUser;validateId user;validateValue cccc "alertTextOk": "* 帐号可以使用.", "alertTextLoad": "* 检查中, 请稍后...", "alertText": "* 帐号不能使用." }, "ajaxUserCall": { "url": "ajaxValidateFieldUser", // you may want to pass extra data on the ajax call //"extraData": "name=eric", "alertText": "* 此名称已被其他人使用", "alertTextLoad": "* 正在确认名称是否有其他人使用,请稍等。" },