Detecting autocomplete on form input with jQuery

前端 未结 9 1802
闹比i
闹比i 2020-12-15 03:50

I have a form that detects if all the text-fields are valid on each keyup() and focus(); if they\'re all valid, it will enable the submit button for the user to press. Howev

9条回答
  •  無奈伤痛
    2020-12-15 04:22

    My issue was detecting auto-fill (via a plugin like lastpass or 1password) as well as the issue described above.

    The solution that worked for me was:

    $(function(){    
        function validate(){
            if($('#email').val() !== '' && $('#password').val() !== '')
                $('#submit').prop('disabled', false);
            else
                $('#submit').prop('disabled', true);
        }
    
        // Validate after user input
        $('#email, #password').on('keyup change', validate);
    
        // Validate on mouse enter of body and login form
        // to catch auto-fills from roboform/1password etc...
        $('body, #loginform').on('mouseenter', validate);
    
        // Validate onload incase of autocomplete/autofill
        validate();
    });
    

    See demo in JSFiddle.

提交回复
热议问题