Came to this article when I was looking for my solution. A non-intrusive way is needed so that people do not enter anything by numbers in one field and nothing but letters, numbers or spaces in other fields. I could not use pattern = "9999" for numbers, as this was not a required field, and people "screamed" if they inserted this field. Likewise, it was not possible to use pattern = "xxx" for alpha / number fields, as I also needed spaces.
Leapfrogging from this article and using javascript that was developed by previous programmers for this client, I came up with these beautiful handlers, thought that I would share if someone needed this elegant solution, and ALSO, because sometimes I forget and will find it again.
Either in the .js file that you include, or enclosed in tags:
function numChecker(e) { if (!e.value.match(/^[0-9]+$/)) { e.value = e.value.substring(0.e.value.length -1 ); e.focus(); return false; } return true; } function charChecker(e) { if (!e.value.match(/^[a-zA-Z0-9\ ]+$/)) { e.value = e.value.substring(0.e.value.length-1); e.focus(); return false; } return true; }
Then your input fields or cfinput will have OnKeyUp="numChecker(this)" or OnKeyUp="charChecker(this)" in their attributes.
As people type, if they enter an invalid character, this script will delete and simply delete that bad character. No additional buttons for clicks or alerts are required.
source share