Validate html text input as it's typed

前端 未结 9 678
再見小時候
再見小時候 2020-11-27 16:36

What\'s the best way of validating an HTML text input as it\'s typed? All ways I know of doing it has some drawbacks:

  1. Using $.keypress you only

9条回答
  •  再見小時候
    2020-11-27 17:15

    Just for another solution to prevent certain characters, without JQuery...

    
    
    ...
    
    function checkInput(e)
    {
        //only alpha-numeric characters
        var ok = /[A-Za-z0-9]/.test(String.fromCharCode(e.charCode));
        if (!ok)
            e.preventDefault();
    }
    

    I don't know if this requires new versions of things or only works on some browsers. Please comment.

提交回复
热议问题