You can check data validity in action and do not add if it's not valid.
when ever user keys in invalid code. i need to show user an error message saying invalid student number and i should not add that record(here onchange event action and onclick event action executing one after other). first onchange event action will execute, inside that method i can know whether keyed in code is valid or not according to that i need to cancel add action.
It's not possible to cancel request, so you should check data validity once again and do not add if it's not valid.