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

Hi Monks,
I have a problem here where I can't use JavaScript to validate form fields, instead I have to use perl, but how do I do an alert for example using perl if some field in my form doesn't have the data I need.
Thanks for the tip!!!

Replies are listed 'Best First'.
Re: Validation in forms using Perl
by dragonchild (Archbishop) on Jun 01, 2005 at 18:55 UTC
    Using standard HTML/CGI techniques, you cannot. However, what you can do is use AJAX. That's a way of using Javascript in the background to communicate with the server. There, you can have Perl do whatever you need it to do and respond back. Then, you go ahead and have the Javascript take whatever action you need it to take.

    This is the best of both worlds - you have the responsiveness of JS and the power of Perl.


    • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
    • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"
Re: Validation in forms using Perl
by mpeters (Chaplain) on Jun 01, 2005 at 21:00 UTC
    I would never rely on Javascript completely for form validation anyway since it can easily be bypassed. I'm not saying it shouldn't be done since it can save you a trip to the server is the user is playing nice.

    To help out with the problem, there are Data::FormValidator and CGI::FormBuilder. The later even helps you do validation in both JS and Perl.

    More people are killed every year by pigs than by sharks, which shows you how good we are at evaluating risk. -- Bruce Schneier
Re: Validation in forms using Perl
by derby (Abbot) on Jun 01, 2005 at 19:32 UTC

    Well ... discounting PerlScript on the client ... you have to validate on the server side. That's pretty much the standard since not too many of us have the luxury of being able to enforce client standards (but my how we try!).

    -derby
Re: Validation in forms using Perl
by dynamo (Chaplain) on Jun 01, 2005 at 20:25 UTC
    You can alert them in the next page by outputting an error instead of whatever else you would have put there if everything had gone according to plan.

    This may or may not be really obvious, but I think it has to be mentioned in this thread: print is all you really need.

    my $error = "Generate your error message here"; print <<EOF; Content-Type: text/html Error: $error EOF
Re: Validation in forms using Perl
by cool_jr256 (Acolyte) on Jun 01, 2005 at 19:30 UTC
    You can validate form fields using perl and display an alert as a html however you cannot do an alert without the use of javascript...(client side)
      Hi. Yes it works. Here is an exerpt of what use for a problem like yours :
      ... &ConfirmBox("OK : Le device " . $o_nom . "a été renommé " . $n_nom . " dans ManagIP."); ... ... # + # You can use the javascript:alert instead + # + sub ConfirmBox { my ($Message) = @_; print "<html><body onload=\"javascript:confirm(\'" . $Message . "\\nCliquer sur OK pour continuer." . "\')\"></body></html>" }
      Hope it'll help.
      --
      cmic. Life helps. Perl Too.
        I think you're confusing the issue. The OP knows he can print javascript/html, but its still the browser that pops up the alert.