Set input as invalid

前端 未结 1 652
故里飘歌
故里飘歌 2020-12-08 14:11

I have two inputs, e.g.

pass:       
pass again:  

        
1条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 14:51

    In the HTMLInputElement interface, there is no such property as valid or invalid.

    You can use the setCustomValidity(error) method with native form validation.

    As for your script, here's a demo that should work in all HTML5 compliant browsers:

    $('input[name=pass2]').keyup(function () {
        'use strict';
    
        if ($('input[name=pass]').val() === $(this).val()) {
            $('#pass_hint').html('match');
            this.setCustomValidity('');
        } else {
            $('#pass_hint').html('mismatch');
            this.setCustomValidity('Passwords must match');
        }
    });
    
    

    Password:

    Verify:

    0 讨论(0)
提交回复
热议问题