Re: How to validate the form in CGI
by Joost (Canon) on Mar 08, 2005 at 11:17 UTC
|
isBlank() is not a predefined function in javascript. With firefox / mozilla you can open the javascript console with the url javascript: and you usually get decent error messages (at least, a lot better than in IE).
Also, it's not realy safe to rely on javascript alone to validate a form. You can read and test input with the CGI module's param() method.
update: and you should print the end_html() at the end of the html, not immediately after the start. :-)
| [reply] [d/l] |
|
|
The mantra around the monastery is always "don't trust Javascript." That is true if a server-side check is omitted altogether, but Javascript can provide a very fast preliminary client-side check that doesn't require the long trip to the server. The JS validation protects the user with a fast check, while the server side protects the internals.
I think both can work in tandem, with the understanding that the server-side validation will always be there.
—Brad "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
| [reply] |
Re: How to validate the form in CGI
by Nevtlathiel (Friar) on Mar 08, 2005 at 14:23 UTC
|
As others have suggested, you might like to look at using CGI as well as javascript to validate the user's input. Since you're new to CGI, I would recommend reading Ovid's CGI Course particularly Chapter Three which is all about security, checking user input and making sure that the input you allow will not mean people can do evil things to you - the first rule of CGI programming is assume that all your users are evil! ;)
The excellent book Beginning Perl also has an Introduction to CGI which doesn't mention javascript but is still pretty useful. | [reply] |
Re: How to validate the form in CGI
by jhourcle (Prior) on Mar 08, 2005 at 12:04 UTC
|
I think the biggest problem is your invalid submit element: <input type="submit" value="Submit" onsubmit=">When you're making any sort of CGI program, especially one that uses JavaScript, you should do the following to debug: - Connect to the CGI
- View the source.
- Look over the source for properly formed and valid HTML.
- Look over the enclosed JavaScript for proper validity. (or run it through a browser that will give good error messages on javascript problems)
That being said, you can never depend on a user having javascript turned on. If there is some sort of validation going on in the form, you should also validate it in the form backend, to make sure that the values are what you expect. (it may be that someone has JS turned off, or someone may be malicious and connect directly to your backend to try to force bad data on you).
| [reply] [d/l] |
|
|
| [reply] |
|
|
Good point -- I shouldn't assume that everyone's using an editor that does syntax highlighting and validation. Some of the browsers have syntax coloring builtin when you view source, and there are a few that have built-in validators (iCab comes to mind). I've personally reset my source viewer with my text editor (BBEdit, as I'm a mac user). For those who haven't been writing HTML for more than a decade (damned <br> tag -- should've just made <address> preserve line endings)
Anyway, there is a validation add-on for Mozilla, and there are even some validators in CPAN, like HTML::Validator and HTML::Tidy
| [reply] [d/l] [select] |
Re: How to validate the form in CGI
by b10m (Vicar) on Mar 08, 2005 at 11:33 UTC
|
It might be overkill for you, but you might like CGI::FormBuilder.
--
b10m
All code is usually tested, but rarely trusted.
| [reply] |
|
|
I've had good experience with this module. Clean, object-oriented design. It supports both client- and server- side validation, in tandem, although personally I only use the server-side stuff. A lot of work went into version 3, although I have not used this either, and it is pretty new.
| [reply] |
Re: How to validate the form in CGI
by manav (Scribe) on Mar 08, 2005 at 11:13 UTC
|
- onsubmit() is given inside the FORM tag....it returns true or false.....
see this
onsubmit event to properly code your JavaScript.
Manav
| [reply] |
Re: How to validate the form in CGI
by mkirank (Chaplain) on Mar 08, 2005 at 12:04 UTC
|
#!perl
use CGI qw(:standard);
use strict ;
use warnings;
my $query = new CGI;
print $query->header;
print start_html(-title=>"hello world", -bgcolor=>"#BOE0E6" );
my $js =<<JS;
<SCRIPT Language="javascript">
function check_answer()
{
if(document.hello.empcode.value==false)
{
alert("Please enter the Software");
document.hello.empcode.focus();
return false;
}
return true;
}
</SCRIPT>
JS
print "$js \n\n";
print $query->startform(-method=>"POST",-action=>"mycgi.cgi",-onSubmit
+=>'return check_answer()',-name=>'hello');
print $query->textfield(-name=>'empcode',
-size=>20,
-maxlength=>20);
print $query->submit(-name=>'submit',
-value=>'submit');
print $query->reset ;
print $query->endform;
| [reply] [d/l] |
Re: How to validate the form in CGI
by markjugg (Curate) on Mar 08, 2005 at 22:42 UTC
|
| [reply] |
Re: How to validate the form in CGI
by metaperl (Curate) on Mar 08, 2005 at 17:36 UTC
|
| [reply] |
Re: How to validate the form in CGI
by Popcorn Dave (Abbot) on Mar 08, 2005 at 16:51 UTC
|
I'm suprised nobody mentioned this, but you really should be untainting your data in addition to checking that it exists. That way if someone puts erroneous data in to your employee number, you'll catch it before it potentially causes any damage. Do a perldoc taint for further reading.
And as other Monks have mentioned, you really should look at using the param method of CGI.pm. It makes life a lot easier when doing this type of thing.
Useless trivia: In the 2004 Las Vegas phone book there are approximately 28 pages of ads for massage, but almost 200 for lawyers.
| [reply] |
Re: How to validate the form in CGI
by kprasanna_79 (Hermit) on Mar 09, 2005 at 09:34 UTC
|
You try out the following perl modules
CGI::Application::ValidateRM;
Data::FormValidator
--prasanna.k
| [reply] |