RegEx for Javascript to allow only alphanumeric

前端 未结 18 1251
轻奢々
轻奢々 2020-11-22 15:13

I need to find a reg ex that only allows alphanumeric. So far, everyone I try only works if the string is alphanumeric, meaning contains both a letter and a number. I just w

18条回答
  •  伪装坚强ぢ
    2020-11-22 15:42

    Try this... Replace you field ID with #name... a-z(a to z), A-Z(A to Z), 0-9(0 to 9)

    jQuery(document).ready(function($){
        $('#name').keypress(function (e) {
            var regex = new RegExp("^[a-zA-Z0-9\s]+$");
            var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
            if (regex.test(str)) {
                return true;
            }
            e.preventDefault();
            return false;
        });
    });
    

提交回复
热议问题