View Full Version : help with form validation please.
Hi,
My contact form has been repeatedly submitted to me with no boxes filled out. I have four form in total buyt if I can get help with this one, I can figure out the others. (he said optimistically).
I need to validate the entries but dunno how to. The three field names are, Realname, Email,Body.
I am thinking that the script below is where the validation can be inserted but if not I can post the parts that are relevant.
sub result {
my $msg;
my @error;
$msg .= "Enquiry for: $dpt_name\n";
foreach (@fields) {
if($query->param("$_") =~ /\|/ || $query->param("$_") =~ /\\/ || $query->param("$_") =~ /\//) {
push @error, "$_ field contains invalid characters"
}
$msg .= join(': ',$_,$query->param("$_"))."\n";
}
if(@error) {
print qq(
Errors:<BR>
);
foreach (@error) {
print "$_<BR>\n";
}
} else {
sendmail($msg);
print qq(
Bazz
MattJakel
07-08-2004, 06:14 PM
You could do this in perl by adding
unless ($realname && $email && $ body) { ##Or whatever the variables are called
print "Location: http://www.yourdomain.com/errorpage.html\n\n";
exit;
} else {
##your normal program
}
But it makes more sense to do it client side with javaScript so that you can have an alert appear but still keep the same page like this:
function checkFields() {
var form = document.forms.yourFormName;
if (form.Realname.value && form.Email.value && form.Body.value) {
form.submit();
} else {
alert("Please fill in all fields!");
}
}
And then replace your submit button with this:
<input type="button" onClick="javascript:checkFields();" value="Submit">
Hope this helps! :)
Matt
EDIT: Ok, I messed up on the javaScript at first, but I fixed it.
Thanks Matt.
Am I right to think that if using server side language, and the user omits to fill a text box, that they will have to start the form from scratch after the server has told them that they did not complete it properly? If so, I can see now how JS would be useful but how do those who have JS turned off survive?
Bazz
Afrow UK
07-09-2004, 12:57 PM
Thanks Matt.
Am I right to think that if using server side language, and the user omits to fill a text box, that they will have to start the form from scratch after the server has told them that they did not complete it properly? If so, I can see now how JS would be useful but how do those who have JS turned off survive?
Bazz
People shouldn't have it turned off, and there's no reason they should do. Almost every website on the internet uses JavaScript in one way or another (you may not even realise it), so you shouldn't worry about that.
You should only worry about those using IE4, which is nill because nobody does (IE5 came with Win95 I seem to remember!)
-Stu
mlseim
07-09-2004, 01:02 PM
You display the webpage with the form and the user
enters data and then clicks submit.
The form runs the CGI script and within the script you
do some validation testing stuff.
If you find a problem, instead of going back to the
webpage (making them start over), display the form
using the CGI script itself, with the good variables in
place and the bad ones, or fields missing, in a red
color font. The CGI script creates the HTML webpage,
with the form ... it can look just like the original page.
That form will call itself (the same CGI script that created
it). And once again, it will test itself.
Mlseim thats what I want to do. But what I have presently, is a form which when filled in (or not), can be sent by hitting the submit button.
help on achieving this would be great and I cn post the whole code so far if need be.
bazz
Afrow UK
07-09-2004, 04:13 PM
Your script could just output an error message with a back link on it, using javascript:history.back(1). In all browsers that I use, form content is saved in the memory so you can go back to it. It's a cheap way to do it, but it works for me.
However, if you want to do it the hard way, you could do it like this:
read(STDIN, $querypost, $ENV{'CONTENT_LENGTH'});
if ($querypost) {
if ($query->param("name") eq $null) {
$query_errors[0] = 1;
$errors = 1;
}
}
if (($errors == 1) or ($querypost eq $null)) {
print qq~
<form name="enter_name" method="post" action="">
<label for="name_id">~;
if ($query_errors[0] == 1) {
print qq~<span style="color: #F00">Enter your name:</span>\n~;
} else {
print "Enter your name:";
}
print qq~ </label><input type="text" name="name" id="name_id" />
<input type="submit" />
</form>
~;
} else {
## actually save submitted data now!
}
It's very messy (and I don't like the method very much) but it works (should do).
As you can see, I'm not using the CGI mod to check if a query exists, because you may be using the query GET method to display the page (which will break the code). That is one reason why I don't use the CGI query() because it serves for both GET and POST submit methods (dunno how you get it to only do one or the other?)
-Stu
mlseim
07-09-2004, 07:23 PM
But what I have presently, is a form which when filled in (or not), can be sent by hitting the submit button.
You can still have a submit button ...
... say this is your script called email.cgi
(sort of summarized here):
-------------------------------------------------------
#cgi code here
#parse variables from the POST method
#check for errors
#no errors ... then email the form
if there are errors, redisplay the form by
calling a subroutine. The subroutine's form method
calls it's own script. The stuff between the q~~ and
the ~; is an exact copy of your HTML webpage that
contains your form. The only thing you'll need to change
after pasting, is the paths to any graphic images, since
this will now be coming from your cgi-bin.
sub form_error
{
print "Content-type: text/html\n\n";
print q~~
<form method=post action='http://www.mysite.com/cgi-bin/email.cgi'>
<p>Your Email address:<br> <input type=text size=25 NaME=sender value='$sender'></p>
<p>Your Name:<br> <input type=text size=25 NaME=username value='$username'></p>
<p>Phone (w/area code):<br> <input type=text size=25 NaME=phone value='$phone'></p>
<p>Subject:<br> <input type=text size=35 NaME=subject value='$subject'></p>
<p>Message, Questions, Comments:<br>
<textarea name=message cols=40 rows=10 value='$message'></textarea></p>
<p><input type=submit value=Submit> Click Here </p>
</form>
~;
}
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.