Your problem description is quite specific - but ultimately what you are talking about is some type of form validation.
Generally form validation falls into two categories:
1. things you can validate purely on the client - because you don't need access to data or logic on the server side,
and
2. things you can only validate on the server - by accessing server data and/or logic.
I don't know which yours falls into. If you have all the information on the page - then you can easily use Javascript for #1. If your problem falls into "requiring server data" - then you can still use JavaScript - but you will need to make an AJAX call... or otherwise you will have to do the validation on the server. Many server side frameworks include form validation as a built i concept in the framework. (such as Struts)