简体   繁体   English

输入验证仅onblur

[英]input validation ONLY onblur

I would like my field validation to occur ONLY when the user leaves the field. 我希望仅当用户离开字段时才进行字段验证。 I've tried the techniques in jQuery validation onblur (see francios wahl's response), but the user experience is that the validation is onblur the FIRST TIME and then reverts to onchange. 我已经尝试了jQuery验证onblur中的技术(请参阅francios wahl的回复),但是用户体验是,验证是在第一时间onblur,然后又恢复为onchange。 Is this expected behavior? 这是预期的行为吗? I can't see why the even would changed. 我不明白为什么偶数会改变。

The entire page can be seen at http://ginof.users.sonic.net/part4-wOutFunction.html 可以在http://ginof.users.sonic.net/part4-wOutFunction.html上看到整个页面

$(function() {

$("#inputForm").validate({
onfocusout: function (valueToBeTested) {
    $(valueToBeTested).valid();
},
rules:
    {
    valueToBeTested: { required: true, digits: true, minlength: 5, maxlength: 5 }
}
}); 

});

This is the default behaviour of the jQuery validation plugin as described on this page 这是因为上描述了jQuery验证插件的默认行为

In addition, once a field was highlighted as being invalid, it is validated whenever the user types something in the field (option onkeyup). 此外,一旦某个字段被突出显示为无效,只要用户在该字段中键入内容(选项onkeyup),该字段便会得到验证。

If you add the option onkeyup set to false then this will cancel this default behaviour as in the snippet below. 如果您将onkeyup选项设置为false,则将取消此默认行为,如下面的代码片段所示。

Also, the validation plugin has a method for evaluating a single element called element ( Details here ), which you may want to consider instead of using .valid() on the element. 另外,验证插件具有一种用于评估单个元素的方法,该元素称为element( 在此处详细信息 ),您可能需要考虑使用该方法,而不是对元素使用.valid()。

 $(function() { $("#inputForm").validate({ onfocusout: function(valueToBeTested) { $(valueToBeTested).valid(); }, onkeyup: false, rules: { valueToBeTested: { required: true, digits: true, minlength: 5, maxlength: 5 } } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.js"></script> <div class="space"> <h1>After the first validation, this reverts from onblur to onchange. Why?</h1> </div> <form id="inputForm"> <label class="center" for="valueToBeTested">Enter the number:</label> <input name="valueToBeTested" type="number" class="center" id="valueToBeTested"> <div id="outputText"></div> <input type="submit" value="calculate" id="triggerButton" class="center"> </form> 

You have a few issues in your code... 您的代码中有一些问题...

$("#inputForm").validate({
    onfocusout: function (valueToBeTested) {
        $(valueToBeTested).valid();
    },
    rules: {
        valueToBeTested: { 
            required: true,
            digits: true,
            minlength: 5,
            maxlength: 5
        }
    }
});
  1. You should not use .valid() within .validate() . 您不应在.valid()使用.valid() .validate() Calling .valid() is overkill... .element() validation simply returns a true/false which is all you need in this case, and the this.element() method is how the plugin's internal default function is written. 调用.valid()是多余的... .element()验证仅返回true/false ,这是您在这种情况下所需的全部, this.element()方法是插件内部默认函数的编写方式。

  2. The argument valueToBeTested which is passed into your onfocusout function only generically represents the element being tested... it is not supposed to represent one particular element. 传递给onfocusout函数的参数valueToBeTested通常表示要测试的element ……不应表示一个特定的元素。 In other words, the argument passed into this function only represents the field being evaluated at any particular time... it does not represent a specific field name. 换句话说,通过这个函数的参数仅代表在任何特定时间被评估的领域......它并不代表一个特定的字段名。

  3. The name valueToBeTested that you've used for your field and to declare your rules is misleading because it only represents the name of the field, not the value of anything. 您用于字段并声明规则的名称valueToBeTested具有误导性,因为它仅表示字段name ,而不代表任何值。 Technically functional, semantically misleading. 技术上的功能,语义上的误导。

the user experience is that the validation is onblur the FIRST TIME and then reverts to onchange 用户体验是,第一次验证是模糊的,然后又恢复为onchange

  1. Technically, it's onkeyup . 从技术上讲,它是onkeyup The validation is "lazy" by default, which is why you only see certain event triggers after the "first time". 默认情况下,验证为“惰性”,这就是为什么您仅在“首次” 之后看到某些事件触发器的原因。 You've already overridden part of the "lazy" validation with your custom onfocusout function. 您已经使用自定义onfocusout函数覆盖了“惰性”验证的一部分。 If you want to disable onkeyup set it to false , otherwise, you can override this function with your own as well. 如果要禁用onkeyup请将其设置为false ,否则,也可以使用自己的方法覆盖此功能。

     $("#inputForm").validate({ onfocusout: function (element) { // <- any "element" being evaluated this.element(element); // <- use '.element()' method }, onkeyup: false, // <- disables all key-up validation rules: { elementName: { // <- NAME of input element required: true, digits: true, minlength: 5, maxlength: 5 } } }); 

DEMO: http://jsfiddle.net/jen8fgts/ 演示: http : //jsfiddle.net/jen8fgts/

Thanks! 谢谢! Adding onkeyup: false, 添加onkeyup:false,

fixed the code. 修复了代码。 Also appreciate the style comments. 也请欣赏样式注释。

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

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