简体   繁体   English

如何仅针对onfocusout事件配置客户端验证

[英]How to configure client side validation only for onfocusout event

Here is the jquery unobtrusive adapter which i am using for client side validation in the mvc application. 这是我在mvc应用程序中用于客户端验证的jquery非侵入式适配器。 Right now the validation is working but it is doing on onkeupevent . 目前,验证工作正常,但它在onkeupevent上进行。 I am looking to have the clientside validation fireup only after the user leaves the field - Could anyone guide me on how to get this working?. 我希望仅在用户离开该领域后才进行客户端验证启动-有人可以指导我如何进行此工作吗? I want to set this configuration only for certain fields not the entire form. 我只想为某些字段而不是整个表单设置此配置。

jQuery.validator.unobtrusive.adapters.add("compareemail", ["otherproperty"], function (options) {
    options.rules["compareemail"] = options.params.otherproperty;
    options.messages["compareemail"] = options.message;
});

jQuery.validator.addMethod("compareemail", function (value, element, params) {

    var reasonElement = $('#personalDetailsEmail');
    var testval = reasonElement.val();
    if (value != testval) {
         return false;
    }
    return true;
});

Thankyou 谢谢

If I understand correctly, this is what you need: 如果我理解正确,这就是您所需要的:

$('.parentOfFields').on( "blur", '.fieldsThatYouNeedAndHaveThisClass', function(){
    console.log($(this))
    //do stuff with $(this)
} )

careful: make sure .parentOfFields exists before this js code is executed, however the same does not apply to .fieldsThatYouNeedAndHaveThisClass, they can be created/added on the fly 注意:在执行此js代码之前,请确保存在.parentOfFields,但该方法不适用于.fieldsThatYouNeedAndHaveThisClass,可以动态创建/添加。

more on blur here: http://api.jquery.com/blur/ 有关模糊的更多信息,请访问: http : //api.jquery.com/blur/
blur means unfocus 模糊意味着不专心

您可以为该特定控件覆盖onblur函数

@Html.TextBoxFor(m => m.UserName, new { onblur = "return false;" })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM