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
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.