Best way to check form's required fields?

michaelsanford

Translator, Web Developer
What's the best way to check that a form's required fields meet criteria before parsing the form to a database?

In my instance there are no rules governing the input (for example, I allow phone numbers to be input as letters because 634-JERY is an acceptable number). I just want to make sure certain fields are not null and a minimum size (ex. phone number at least 7 digits).

The two major issues are that the correctly completed form fields should remain filled in. Also I'd like to avoid JS alert windows, since some text readers (for persons with visual impairments) may not understand them. As a result, I'd like to print a message at the top of the page.

So I'm probably looking at PHP eh?
 
Yep, sounds like server-side is the way to go, perhaps with some javascript client side as a shortcut for those that support it. This way you can avoid roundtrips to the server to do the validation (so quicker for users) but you still get the validation done on the server if the javascript doesn't happen for whatever reason.

I guess which server-side technology you use depends on what you are used to and perhaps more importantly what you have available to you.

For a personal project I'm developing a Java Servlet that handles all form submissions and then validates the form data against simple XML configuration files (one per form) before deciding whether to proceed or return to the form with appropriate error messages. The XML file will determine which fields are required, basic validation rules (e.g. field must contain 8 characters etc etc), and will also hold appropriate error messages to display back to the user.
 
I've managed to figure out a way to do it, using PHP include(""); statements nested within if-else.

The PHP script loads, and checks to see if a special variable is set. if it isn't it assumes the form is fresh and doesn't process it. Otherwise it processes it. Using include(""); allows me to pass the correct variables back to the form if only a few are missing.

It may not be the most efficient, but it does provide for the best accessibility and compatibility, which is what I was going for.

Thanks for the replies guys!
 
Back
Top