View Full Version : Form Validation?
Omnis
05-18-2008, 09:22 AM
For a while I've been thinking on how to go about validating a form using JavaScript (client-side validation) and Perl (server-side validation). There are a few issues I can't get my head around so I was hoping someone here has run into something similar and could help me :).
What I want to be able to do is run the form through client-side validation first and return errors if there are any, then re-validate if required, but if everything on the form validates than I want to be able to send the form for server-side validation, but the thing is what happens if someone has JavaScript turned off, I would then want the form sent for server-side validation and return errors if necessary. Why I want it done this way is to reduce server-side processing.
My biggest issues are:
Can JavaScript communicate with Perl and if so how?
If JavaScript is turned off, how do I know and how do I activate server-side validation?
Can I trust client-side validation?
Cheers :thumbsup:
in question order:
1.Can JavaScript communicate with Perl and if so how?
nope. JS is client side (in the browser) and perl is server-side (on the server)
2. If JavaScript is turned off, how do I know and how do I activate server-side validation?
You can't do it like that (read on).
3. Can I trust client-side validation?
nope.
Basically, you can set up client side validation as a way of reducing errors in the genuine visitors input. But when that all gets sent to the server you need, anyway, to do server-side validation so that no matter whether the data came from a genuine person, or from a spammer/pleb, it will be validated.
hth
bazz
KevinADC
05-18-2008, 05:51 PM
javascript and perl can communicate, but not directly. You use a CGI form to send javascript data to a server side perl script.
I believe you can check if javascript is on or off and redirect the client, ask in the javascripting forum.
But you should always use server side validation. Use the client side too, that will still help reduce server usage, but never assume that because javascipt is enabled it will be sufficient to send validated data to your perl script.
I believe you can check if javascript is on or off and redirect the client, ask in the javascripting forum.
you might like to look into <noscript></noscript> tags. Whatever is inside of those, will operate if js is switched off but it won't if it is switched on and so you could put a redirect in there. And yeah, I agree, check in the JS forum.
bazz
chaosprime
05-19-2008, 09:18 PM
If you want to get really slick, make yourself a class that abstractly models data validation requirements (like you might say $obj->add_validation(new DataValidation('required')); $obj->add_validation(new DataValidation('less than', 100)); and so on). Have this class able to 1) test its requirements against input on the server side 2) render its requirements as JavaScript for pushing out to the client side.
Then you can have your validation rules defined in a single place for maintainability (hopefully as part of a useful OO model of your database schema), get the UI benefits of client-side validation and fulfill the de rigeur requirement of server-side validation.
Omnis
05-22-2008, 05:10 PM
Thanks for all the responses so far :thumbsup:
One other question; if the form goes through client-side validation and returns errors to the user, will it still be sent to the perl/cgi script for processing or does it need to pass client-side validation first?
chaosprime
05-22-2008, 05:17 PM
Client-side validation doesn't pass back to the server until it's clear; otherwise there's no point.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.