简体   繁体   English

jQuery Validate-在焦点/按键上触发自定义'addMethod'

[英]jQuery Validate - Fire custom 'addMethod' on focus / keypress

I've created a couple of custom validation methods for jQuery Validate. 我为jQuery Validate创建了两个自定义验证方法。 One is used for password strength testing. 一种用于密码强度测试。 I'd like this to fire when input is first focused so it validates as the user types giving realtime feedback, noy just on form submission. 我希望在首先关注输入时触发此操作,因此它可以在用户类型提供实时反馈时进行验证,仅在表单提交时有效。

EDIT: The validation only runs when the form is submitted, I want it to run as the user types their password so they get real-time feedback about the password's strength. 编辑:验证仅在提交表单时运行,我希望它在用户键入密码时运行,以便他们获得有关密码强度的实时反馈。

Ideally I only want this real-time validation to work on selected fields. 理想情况下,我只希望此实时验证在选定字段上起作用。 Ie the account email address shouldn't behave this way before the form is submitted or it'll keep alerting them their address in invalid before they finish typing it. 也就是说,在提交表单之前,该帐户的电子邮件地址不应该这样操作,否则在输入完该表单之前,它将不断提醒他们其地址无效。 However if it can only work when applied to all elements I'll accept it for this form. 但是,如果仅当将其应用于所有元素时才起作用,那么我将接受这种形式的表单。

So, is it possible to call a jQuery Validate additional method on focus/keyup before the form has been submitted? 因此,是否可以在提交表单之前在焦点/键上调用jQuery Validate附加方法?

Here's my new method that I want to call. 这是我要调用的新方法。

$.validator.addMethod('passwordStrength', function(password, element, param) {
// add our password test to
if (typeof zxcvbn === 'function') {
    var passwordMeter = $('#' + $(element).attr('id') + 'meter');
    var passwordMeterStrength = passwordMeter.find('.strength');
    var passwordMeterTrack = passwordMeter.find('.track');
    var passwordMeterBar = passwordMeterTrack.children();
    var strengthValues = ['Very weak', 'Weak', 'Average', 'Good', 'Strong'];
    //passwordMeter

    // get values of the other inputs in this form
    var otherinputs = [];
    if (param) {
        for (var input in param['user_inputs']) {
            // get the value and add it to an array
            otherinputs.push($(param['user_inputs'][input]).val());
        }
    }

    // run the test!
    var strength = testPasswordStrength(password, otherinputs);

    // show the strength of the password
    passwordMeterStrength.text(strengthValues[strength.score]);

    // change the class of our bar. nice browsers will animate this in CSS!
    passwordMeterBar.removeClass().addClass('bar strength' + strength.score);


    // we must be two or better
    if (strength.score > 1) {
        return true;
    } else {
        return false;
    }
} else {
    console.error('strength test is not loaded');
}
 }, '');

I ended up moving the main workings of the method into a separate function that is called by my method. 我最终将方法的主要工作移到了由我的方法调用的单独函数中。 I then binded the function to the password input. 然后,我将该功能绑定到密码输入。 I chose the input event as this fires on keypress, and on change - it's very flexible but often forgotten. 我选择input事件是因为它会在按键和更改时触发-它非常灵活,但经常被遗忘。

$.validator.addMethod('passwordStrength', function(password, element, param) {
    return passwordStrength(password, element, param);
}, '');

passwordEl.on('input', passwordStrength());

passwordStrength is the same as in my question. passwordStrength与我的问题相同。

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

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