Hi Monks, Can anybody help me set up validation for my form. I want it to alert the user if fields are left blank.

At the moment when you press the submit button, it goes to the subroutine add_record_two, but I would like it to go through validation first. Here is the code that generates the form.

my %form = Vars(); if ($form{'action'} eq "addrecord") { &add_record; } elsif ($form{'action'} eq "add_record_two") { &add_record_two; } sub header { $title = shift; print qq~ <HTML> <HEAD> <TITLE> $title </TITLE> <basefont face="Trebuchet MS,Arial,Helvetica" size="2"> </HEAD> <BODY bgcolor="#FFFFFF"> <CENTER> <H1>CHGMANT V2</H1> ~; } sub footer { print qq~ <TABLE border=0 cellspacing=3 cellpadding=3> <TR> <TD><a href="chgmant_v4.cgi?action=addrecord"><FONT COLOR="#00 +0000" size=-1>Add A Record</a></TD> <TD><a href="chgmant_v4.cgi?action=edit"><FONT COLOR="#000000" + size=-1>Modify a Record</TD> <TD><a href="chgmant_v4.cgi?action=delete"><FONT COLOR="#00000 +0" size=-1>Delete a Record</TD> <TD><a href="chgmant_v4.cgi?action=search"><FONT COLOR="#00000 +0" size=-1>Search</TD> </TR> </TABLE> </CENTER> </BODY> </HTML> ~; } sub build_record_page { my (%record) = @_; my ($val) = ""; my ($html) = qq~<TABLE border=1 bgcolor="#FFFFFF" cellspacing=0 cellpa +dding=4> <TR bgcolor="#C0C0C0"> <TD colspan=2><CENTER><font size=-1> Record </CENTER>< +/TD> </TR>~; foreach $obj (@db_fields) { if ($obj eq $db_key) { next; # ID's are dynamically made } $html .= qq~<TR bgcolor="#DDDDDD"><TD><FONT SIZE=-1>$d +b_name{$obj}</TD><TD>~; if ($db_type{$obj} eq "text") { # Makes the text box if ($record{$obj}) { $val = qq~ VALUE="$record +{$obj}"~; } else { $val = ""; } $html .= qq~<INPUT TYPE="text" NAME="$obj" siz +e="20" $val>~; } if ($db_type{$obj} eq "drop" && $db_name{$obj} eq "Use +rID:") { # Makes the dropdown box if ($record{$obj}) { $val = qq~ VALUE="$record +{$obj}"~; } else { $val = ""; } print "$record{$obj} $id\n"; # +debug $html .= popup_menu ( -name = +> $obj, -value +s=>\@users); } if ($db_type{$obj} eq "drop" && $db_name{$obj} eq "Typ +e Of Change:") { # Makes the dropdown box if ($record{$obj}) { $val = qq~ VALUE="$record +{$obj}"~; } else { $val = ""; } print "$record{$obj} $id\n"; # +debug $html .= popup_menu ( -name = +> $obj, -value +s=>\@changetype); } elsif ($db_type{$obj} eq "textarea") { # Makes the tex +tarea if ($record{$obj}) { $val = qq~$record{$obj}~; + } else { $val = ""; } $html .= qq~<TEXTAREA NAME="$obj" ROWS="4" COL +S="40">$val</TEXTAREA>~; } $html .= qq~</TD></TR>~; } $html .= "</TABLE><P>"; print $html; } sub add_record { &header("Add a Record"); print qq~ <CENTER> <FORM METHOD=POST> <INPUT TYPE="hidden" NAME="action" VALUE="add_record_two">~; &build_record_page; print qq~ <CENTER> <INPUT TYPE="submit" value="Add Change"></CENTER> <CENTER> </FORM>~; &footer; } sub add_record_two { $form{$db_key} = &get_next_id(); my ($line) = &make_data(%form); open (DATABASE, ">>".$file); if ($flock) { flock(DATABASE, 2) } print DATABASE $line; close (DATABASE); &header("Add a Record Successful - $id"); print qq~<CENTER>Here Is Your Record</CENTER><p>~; my (%result) = get_record($form{$db_key}); print Dumper(%result); if (%result) { &build_record_page(%result); } else { print "Error - No Record Added"; } &footer; }

Thanks

In reply to Form validation using CGI by wishartz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.