How to prevent user from entering special characters in text box when length is 0?

前端 未结 3 1802
盖世英雄少女心
盖世英雄少女心 2020-12-11 01:13

I have the following code which prevents user from entering space when the length is 0. Now, how can I prevent user from entering all special characters(anything other than

3条回答
  •  清歌不尽
    2020-12-11 01:39

    This is what you are looking for:

    $('#DivisionName').bind('keypress', function(e) {
    
        if($('#DivisionName').val().length == 0){
            var k = e.which;
            var ok = k >= 65 && k <= 90 || // A-Z
                k >= 97 && k <= 122 || // a-z
                k >= 48 && k <= 57; // 0-9
    
            if (!ok){
                e.preventDefault();
            }
        }
    }); 
    

    or see here: http://jsfiddle.net/D4dcg/

提交回复
热议问题