I’m trying to validate an input field by making sure that only text, spaces and numbers can be inputted. I thought that the easiest way to do this would be using a Regular Expression by doing something like this:
function checkVal() {
var input = document.getElementById("input").value;
if (input.match(/[a-zA-Z0-9]+$/) == null) {
alert("error");
}
}
More specifically I need to make sure that the underscore character isn’t within this field (this is really important). From testing this it doesn’t seem to be working properly but I think this is more so because I’m maybe using a regular expression for the wrong purpose and not because it’s syntactically incorrect.
Would it just be easier for me to write a function to exclude the “underscore” character when typing in that input box?
EDIT: Just to make this a bit clearer, I only want to accept letter, numbers and spaces.
Don’t forget to include the beginning of the string,
^(and the space):