CustomValidator ajax,jQuery validator and a custom rule that uses AJAX

这篇博客介绍了一种优化jQuery validate.js库的策略,通过创建延迟来避免频繁调用验证函数,特别是对于涉及服务器端验证(如AJAX调用)的情况。作者提供了一个实用函数`jqvAsyncValid`,它在用户停止输入一段时间后才执行验证,实现了实时验证。此外,还展示了如何添加自定义的异步验证方法和错误处理。该方法有助于提高性能,减少服务器负担。
摘要由CSDN通过智能技术生成

Here's my "old school" hack...

Below a utility function that allows the use of "asynchronous" validations with "jquery.validate.js" library. This function creates a delay between user keystrokes otherwise the validation function "validFunc" will be called "all time" which is not very performative in some circumstances and especially problematic for functions that perform validations on "serverside"/"backend" (ajax calls basically). In this way the "validFunc" validation function is only called when the user stops typing for a certain period of time which also allows a "realtime" validation ("onkeyup": true on jqv settings) as it occurs while the user is typing.

IMPORTANT: Validations involving the use of the "jqvAsyncValid" function should always be the last ones to avoid conflicts with the others due to the asynchrony.

{

[...]

"rules": {

"field_name": {

"required": true,

"maxlength": 12,

"minlength": 4,

// NOTE: Validation involving the use of the "jqvAsyncValid" function. By Questor

"my_custom_ajax_validation": true

},

[...]

}

ANSWER'S CODE:

// NOTE: Adds the custom validation "my_custom_ajax_validation". By Questor

$.validator.addMethod("my_custom_ajax_validation", function (value, element) {

return jqvAsyncValid(element, "my_custom_ajax_validation", myValidationFunc, this);

}, "My error message!");

// NOTE: My validation function. By Questor

function myValidationFunc(domElement) {

if (someFuncWithAjaxCall(domElement.value) == "ALL_RIGHT!") {

return true;

} else {

return false;

}

}

// NOTE: Global "json" variable that stores the "status" ("isValid") and cycle control

// ("toCtrl") of asynchronously validated elements using the "jqvAsyncValid" function.

// By Questor

var jqvAsyncVState = {};

// NOTE: A utility function that allows the use of asynchronous validations with

// "jquery.validate.js". This function creates a delay between one user keystroke and

// another otherwise the validation function "validFunc" will be called "all time"

// which is not very performative in some circumstances and especially problematic

// for functions that perform validations on the serverside/backend (ajax calls basically).

// In this way the "validFunc" validation function is only called when the user stops

// typing for a certain period of time, which also allows a "realtime" validation

// as it occurs while the user is typing. By Questor

// [Ref .: https://jqueryvalidation.org/ ]

//. domElement - DOM element informed by jqv in the "addMethod" for the anonymous

// function;

//. asyncRuleNm - Validation name added via "addMethod";

//. validFunc - Function that will do the validation. Must have the signature

// "funcName(domElement)" returning "true" for valid and "false" for not;

//. jQValidInst - Instance of the current jqv within "addMethod". It is usually

// denoted by "this";

//. toInMsecs - "Timeout" in "milliseconds". If not informed the default will be

// 1500 milliseconds. Be careful not to use a very short timeout especially in

// "ajax" calls so as not to overload the serverside/backend.

// Eg.: `return jqvAsyncValid(element, "my_custom_ajax_validation", myValidationFunc, this);`.

function jqvAsyncValid(domElement, asyncRuleNm, validFunc, jQValidInst, toInMsecs) {

if (typeof toInMsecs === "undefined" || toInMsecs === "") {

toInMsecs = 1500;

}

var domEKey = jQValidInst.currentForm.id + domElement.name;

// NOTE: The validation messages need to be "displayed" and "hidden" manually

// as they are displayed asynchronously. By Questor

function errMsgHandler() {

if (jqvAsyncVState[domEKey]["isValid"]) {

// NOTE: If the current error message displayed on the element was that

// set in the rule added via "addMethod" then it should be removed since

// the element is valid. By Questor

// [Ref.: https://stackoverflow.com/a/11652922/3223785 ,

// https://stackoverflow.com/a/11952571/3223785 ]

if (jQValidInst.errorMap[domElement.name] == $.validator.messages[asyncRuleNm]) {

var iMsgNow = {};

iMsgNow[domElement.name] = "";

jQValidInst.showErrors(iMsgNow);

}

} else {

var iMsgNow = {};

// NOTE: If the element is invalid, get the message set by "addMethod"

// for current rule in "$.validator.messages" and show it. By Questor

iMsgNow[domElement.name] = $.validator.messages[asyncRuleNm];

jQValidInst.showErrors(iMsgNow);

}

}

if (!jqvAsyncVState.hasOwnProperty(domEKey)) {

// NOTE: Set the global json variable "jqvAsyncVState" the control attributes

// for the element to be asynchronously validated if it has not already been

// set. The key "domEKey" is formed by the "id" of the "form" that contains

// the element and the element's "name". By Questor

jqvAsyncVState[domEKey] = {

"toCtrl": null,

"isValid": undefined

};

}

var useOnKeyup = true;

// NOTE: The "onblur" event is required for "first validation" that only occurs

// in a "blur" event - this is inherent to jqv - and for situations where the

// user types very fast and triggers "tab" and the event "onkeyup" can not deal

// with it. By Questor

domElement.onblur = function (e) {

jqvAsyncVState[domEKey]["isValid"] = validFunc(domElement);

errMsgHandler();

useOnKeyup = false;

}

if (useOnKeyup) {

// NOTE: The strategy with the event "onkeyup" below was created to create

// a "delay" between a "keystroke" and another one otherwise the validation

// function "validFunc" will be called "all time" which is not very performative

// in some circumstances and especially problematic for functions that perform

// serverside/backend validations (ajax calls basically). In this way the

// "validFunc" validation function is only called when the user stops typing

// for a certain period of time ("toInMsecs"). By Questor

domElement.onkeyup = function (e) {

// NOTE: Clear the "toCtrl" if it has already been set. This will

// prevent the previous task from executing if it has been less than

// "toInMsecs". By Questor

clearTimeout(jqvAsyncVState[domEKey]["toCtrl"]);

// NOTE: Make a new "toCtrl" set to go off in "toInMsecs" ms. By Questor

jqvAsyncVState[domEKey]["toCtrl"] = setTimeout(function () {

jqvAsyncVState[domEKey]["isValid"] = validFunc(domElement);

errMsgHandler();

}, toInMsecs);

};

}

return jqvAsyncVState[domEKey]["isValid"];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值