This is the function you are looking for
function validateAnswer(src) {
var questions = document.getElementById("questions");
var rule = questions.options[questions.selectedIndex].value;
if(rule=="no_numbers") src.value = src.value.replace(/\d/g, '');
if(rule=="no_letters") src.value = src.value.replace(/\w/g, '');
}
just send the input field reference to the function and set it to onkeyup
event instead:
<input type="text" name="answer" onkeyup="validateAnswer(this);" />
you should also hook the onchange
event of the selectbox to reset the value of the input box. I suggest you also consider the HTML5 pattern attribute
. See