Changed event listener to keydow instead of keyup. Previous version using keyup meant that keydown would trigger closing of alert box and return focus to the textbox, which would then trigger the entire thing again.