Your trainer will now demonstrate and discuss the use of CGI for validation of data entered into a web form. An example form is in your public_html directory as validate.html and the validation CGI script is available in your cgi-bin directory as validate.cgi.
#!/usr/bin/perl -w use strict; use CGI 'param'; print "Content-type: text/html\n\n"; my @errors; push (@errors, "Year must be numeric") if param('year') =~ /\D/; push (@errors, "You must fill in your name") if param('name') eq ""; push (@errors, "URL must begin with http://") if param('url') !~ m!^http://!; if (@errors) { print "<h2>Errors</h2>\n"; print "<ul>\n"; foreach (@errors) { print "<li>$_\n"; } print "</ul>\n"; } else { print "<p>Congratulations, no errors!</p>\n"; } |
Open the form for the validation program in your browser. Try submitting the form with various inputs.