lgjut has asked for the wisdom of the Perl Monks concerning the following question:

Dear perlmonks:

I have a problem.I use java script function validateForm() to check form input:

print start_multipart_form(-name=>'form1', -onSubmit=>"validateForm()", -method=>"post"),"\n";
it can work ,but cannot return original page when error.
so I change onSubmit=>"return validateForm()",but it cannot get form params.Why?
I use Apache 1.3.12 and IE5, the script as following :
#!/usr/local/bin/perl # Use JavaScript to validate fill-out # forms. use CGI qw(:standard); # Here's the javascript code that we include in the document. $JSCRIPT=<<EOF; // validate that the user is the right age. Return // false to prevent the form from being submitted. function validateForm() { var ret = validateEmail(document.form1.email); if (! ret) { return false; } ret = checkFirstName(document.form1.firstname); if (! ret) { return false; } ret = checkLastName(document.form1.lastname); if (! ret) { return false; } //document.form1.state = 'valid'; //document.forms[0].submit(); return true; } // make sure that the contents of the supplied // field contain a valid date. function validateEmail(element) { //alert(element.value); if (element.value.indexOf("@")<0 ) { alert("Please valid your e-mail address"); element.focus(); element.select(); return false; } return true; } function checkFirstName(element) { if (element.value.length == 0 ) { alert(element.name+"can not be null."); element.focus(); } } function checkLastName(element) { if (element.value.length == 0 ) { alert(element.name+"can not be null."); element.focus(); } } EOF ; # here's where the execution begins print header; print start_html(-title=>'Untitled Document', -head => meta({ -http_equiv => 'Content-Type', -content => 'text/html; charset=iso-8859-1'}), -script=>$JSCRIPT); print_prompt(); print_response() if (param); print end_html; sub print_prompt { #post return print start_multipart_form(-name=>'form1', -onSubmit=>"validateForm()", -method=>"post"),"\n"; print table({-border=>0,-width=>"75%"},"\n", Tr(td({-width=>"32%"},div({-align=>"right"},'First Name: ')) , +td({-width=>"68%"},input({-type=>'text',-name=>'firstname'})) ), Tr( [ td([div({-align=>"right"},'Last Name:'),input({-type=>'text +',-name=>'lastname'})]), td([div({-align=>"right"},'Email Address: '),input({-type=> +'text',-name=>'email'})]), td([div({-align=>"right"},'File One: '),input({-type=>'file +',-name=>'fileone'})]), td([div({-align=>"right"},'File Two: '),input({-type=>'file +',-name=>'filetwo'})]), td([div({-align=>"right"},'File Three: '),input({-type=>'fi +le',-name=>'filethree'})]) ]) ); print hidden('state','init'); print submit(),"\n"; print end_form; } sub print_response { return unless(param('firstname')); return unless(param('lastname')); $value = param('email'); return unless($value =~/@/); my $i=0; my $SAVE_DIRECTORY = "c:/temp"; foreach $key (param()) { next unless $key =~ /file/; my $file_name = param($key); next unless length($file_name)>0; $file_name =~ /([^\\\/]+)$/; $SaveFilename = $1; unless (open(OUTFILE, ">$SAVE_DIRECTORY/$SaveFilename")){ print "Cannot open file $SAVE_DIRECTORY/$SaveFilename for + upload.\n",p;; next; } while ($Bytes = read($file_name,$Buffer,1024)) { print OUTFILE $Buffer; } close(OUTFILE); close($file_name); print "Upload file to $SAVE_DIRECTORY/$SaveFilename.\n",p; $i++; } print "Total file count $i .\n"; }

Replies are listed 'Best First'.
Re: A problem use CGI.pm
by bart (Canon) on Oct 26, 2003 at 02:35 UTC
    ... so I change onSubmit=>"return validateForm",but it cannot get form params.Why?
    Well, at least one things wrong with your system, for sure, and it's this: Javascript functions require that you use parens to call them. Otherwise you'd just return a reference to the function. As if you were to return \&foo instead of foo() in Perl.
      Sorry ,it is a input error .But I did use
      -onSubmit=>"return validateForm()"
      in my test code.
Re: A problem use CGI.pm
by kutsu (Priest) on Oct 26, 2003 at 15:50 UTC

    Please forgive me if I read your code wrong, Javascript isn't my first language. Couldn't you remove the Javascript and do this.

    #!/usr/local/bin/perl -wT use CGI qw(:standard); use strict; my $num = param('number'); #has to be numeric my $let = param('lets'); #has to be letters my $both = param('both'); #can be letter or number die "Invalid datatype for num" if $num =~ /[^0-9]/; die "Invalid datatype for let" if $let =~ /[^a-zA-Z]/; die "Invalid datatype for both" if $both =~ /[^0-9a-zA-Z]/; die "num can not be null" unless defined $num; die "let can not be null" unless defined $let; die "both can not be null" unless defined $both;

    Many other security measures could be taken as well, I suggest you check out Ovid's Course in the Tutorials section for better examples.

    "Pain is weakness leaving the body, I find myself in pain everyday" -me

      Thank you for so kind reminder.
      The reason I use JavaScript is I want to check user input at client side. It can give user response immediately.

        Many people surf with Javascript disabled.

        Thus, you'll have to verify the input server-side anyway.

        Why do it twice?