Javascript--it runs on the browser while the data is being entered on the form. Your php script (the destination of the form) only gets control when the user clicks on a submit button and the data is sent to it.
But beware! The user can (at the least) turn off Javascript at the browser, and (even worse) deliberate post things that might mess you up. If the validation you are doing is important enough to bother writing in the first place, you really need to do it in your php code.
In general, Javascript is a nice adjunct (i.e. you can validate field contents field-by-field, update SELECT lists based on the contents of other fields, and otherwise be quite helpful to the well-intentioned user) but for true safety you have to repeat the real field validation at the server just to be sure.