Forum Moderators: coopster & phranque

Message Too Old, No Replies

How to add required field

html to cgi

         

wm2007

9:03 am on Sep 4, 2006 (gmt 0)

10+ Year Member



I have a Perl script that gets input from form fields in a html page.

I would like to add some lines of Perl code to secure that all fields are present/sent from html page.

Any suggestions?

Thanks :-)

mikeyb

9:47 am on Sep 4, 2006 (gmt 0)

10+ Year Member



Could try something like:

my $name = param('name');

if (length($name) == 0) {
# $name is empty, do stuff!
} else {
# $name has something, do stuff!
}

wm2007

10:01 am on Sep 4, 2006 (gmt 0)

10+ Year Member




Thank you for your suggestion mikeyb, I will try it out later today :-)

perl_diver

7:57 pm on Sep 4, 2006 (gmt 0)

10+ Year Member



mikeyb's suggestion might work, but it will also let things like all spaces get by, which is probably not what you want. You should properly validate all your form fields, even the ones users typically would not enter data for, like hidden form fields. Form field validation can be a bit tedious if there are lots of fields, but it is necessary to avoid later problems with unexpected data.

wm2007

9:33 pm on Sep 4, 2006 (gmt 0)

10+ Year Member



Thanks a lot perl_diver, could you direct me to any information on how to validate my form fields?

The script I would like to "secure" only have 3 required fields: name, email and message. The e-mail field should only allow one e-mail address, the message field should only allow letters and numbers (and nothing else) and I need the script to filter out: \n, \r, bcc:/i, cc:/i, content-type:/i, etc. Also I would like to prevent "hackers" from removing any of the three required fields.

:-)

perl_diver

4:22 am on Sep 5, 2006 (gmt 0)

10+ Year Member



for the email address I would use the Email::Valid module:

[search.cpan.org...]

Ask your host to install it if it's not already installed.

If the message filed really can only contain numbers and letters (but not spaces or punctuation:


if ($message =~ /[\W_]/ or $message eq '') {
do something like print an error message and exit the script
}

what about the name field?

>> Also I would like to prevent "hackers" from removing any of the three required fields.

You can't do that, you can only check if the field has a value or is undefined.

wm2007

10:52 pm on Sep 5, 2006 (gmt 0)

10+ Year Member



Thank you very much perl_diver :)

My script are now more secure