I am having trouble understanding why this regex keeps returning false. I have an onkeydown event that should trigger it when pressing the 'w' key, but it doesn't seem to be working.
var keyGLOB = '';
function editProductSearch (event) {
console.log(keyGLOB);
var key = String.fromCharCode(event.keyCode);
if(event.keyCode == 8) {
keyGLOB = keyGLOB.substring(0, keyGLOB.length -1);
return false;
}
var regex = /^[a-zA-Z0-9]+$/;
if(regex.test(key)) {
if(regex.test(keyGLOB)) {
keyGLOB += key;
searchDb(keyGLOB.toLowerCase());
} else {
alert('letters and numbers only!');
return false;
}
} else {
alert('letters and numbers only!');
return false;
}
}
HTML:
<p><input id='product-edit-search' onkeydown='editProductSearch(event)'></p>