Regex for allowing alphanumeric,-,_ and space

后端 未结 7 2237
北荒
北荒 2020-12-04 16:43

I am searching for a regular expression for allowing alphanumeric characters, -, _ or spaces in JavaScript/jQuery. I tried Googling but wasn\'t able to find that.

Ca

相关标签:
7条回答
  • 2020-12-04 17:35
    var regex = new RegExp("^[A-Za-z0-9? ,_-]+$");
                var key = String.fromCharCode(event.charCode ? event.which : event.charCode);
                if (!regex.test(key)) {
                    event.preventDefault();
                    return false;
                }
    

    in the regExp [A-Za-z0-9?spaceHere,_-] there is a literal space after the question mark '?'. This matches space. Others like /^[-\w\s]+$/ and /^[a-z\d\-_\s]+$/i were not working for me.

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