Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox form validation

Status
Not open for further replies.

xmassey

Programmer
Apr 9, 2007
62
0
0
GB
I have created a script which validates all sorts of information inputted via a HTML form. The only problem I have is with the checkboxes. I need to ensure that only 2 checkboxes out of the 5 are selected. To do this I have given each checkbox a value of 1 therefore by adding all the checkboxes values, if they are equal to one then the form is valid. Below is the checkbox part of my code

$valuesa = param('option1');
$valuesb = param('option2');
$valuesc = param('option3');
$valuesd = param('option4');
$valuese = param('option5');

$valuescalc = $valuesa + $valuesb + $valuesc + $valuesd + $valuese;

if ($valuescalc eq '2') {
#PASS
$one = 'aa';
}

#FAIL
else {
print "Content-type: text/html\n\n";
print "<font face=arial size=2><P><b>Error 1 - $valuescalc (Choose 2 options)</b></font>";

The problem I have is that the code seems to work, and I can display that i.e. if I have 3 checkboxes ticked then $valuecalc is equal to 3. However, if I select 2 checkboxes and try to validate, I get an Error 500.

If you would like to see the form in action, here is the web address:


Thanks for all your help!
 
You should use == instead of eq.

If it is successful does it do anything? In the #Pass section you set a variable but you don't do anything else. I would printer your Content-type and a success meassage (I would probably move the content-type to the top anyway since you need it either way). If your using CGI i think "THINK!" there is a way to tell it to print it for you.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Thanks for your quick response. I just tried using == and I still get an Error 500 when I select 2 checkboxes.

Bear in mind that there is code before and code after. I have just selected and pasted the relavent bits. I have also tried printing a statement if the code is valid rather than saying that $one = 'aa'.

Any other suggestions? How would you validate a group of 5 checkboxes to check if only 2 are checked?

Here is my full code if you wish to see, and you can obviously view the source code for the form at if you wish





#! /usr/bin/perl
use strict;
use CGI ':standard';

############################################################
#######################
## Declare variables ##
#######################

####################
## Form variables ##
####################
my ($valuesa, $valuesb, $valuesc, $valuesd, $valuese);
my ($valuescalc);
my (@values2, $values3, $values4, $values5, $values6);

##########################
## Evaluation variables ##
##########################
my ($one, $two, $three, $four, $five, $six);
##########################################################

##########################################################
#####################
## Connect to form ##
#####################
$valuesa = param('option1');
$valuesb = param('option2');
$valuesc = param('option3');
$valuesd = param('option4');
$valuese = param('option5');

@values2 = param('menu1');
$values3 = param('text1');
$values4 = param('text2');
$values5 = param('text3');
$values6 = param('textbox1');
##########################################################

##########################################################
################################
## A) Checkboxes (2 selected) ##
################################
$valuescalc = $valuesa + $valuesb + $valuesc + $valuesd + $valuese;

if ($valuescalc == '2') {
$one = 'aa';
}

else {
print "Content-type: text/html\n\n";
print "<font face=arial size=2><P><b>Error 1 - $valuescalc (Choose 2 options)</b></font>";
}

##########################################################

##########################################################
##########################
## B) Menu (1 selected) ##
##########################
if (param('menu1') eq '1') {
print "<font face=arial size=2><P><b>Error 2 - @values2 (Choose 1 option)</b></font>";
}

elsif ((@values2 eq '2') || (@values2 eq '3') || (@values2 eq '4') || (@values2 eq '5') || (@values2 eq '6')) {
$two = 'bb';
}

##########################################################

##########################################################
######################
## C) Text 1 (word) ##
######################
if ($values3 eq "") {
print "<font face=arial size=2><P><b>Error 3 - $values3 (Type text)</b></font>";
}

else {
$three = 'cc';
}

##########################################################

##########################################################
#######################
## D) Text 2 (email) ##
#######################
if (($values4 =~ /@/) && ($values4 =~ /./)) {
$four = 'dd';
}

else {
print "<font face=arial size=2><P><b>Error 4 - $values4 (Type an email address)</b></font>";
}

##########################################################

##########################################################
###########################
## E) Text 3 (telephone) ##
###########################
if ($values5 > 99999999999) {
print "<font face=arial size=2><P><b>Error 5 - $values5 (Type a telephone number)</b></font>";
}

elsif ($values5 =~ /\d\d\d\d\d\d\d\d\d\d\d/) {
$five = 'ee';
}

else {
print "<font face=arial size=2><P><b>Error 5 - $values5 (Type a telephone number)</b></font>";
}

##########################################################

##########################################################
#######################
## F) Textbox (word) ##
#######################
if ($values6 eq "") {
print "<font face=arial size=2><P><b>Error 6 - $values6 (Type text)</b></font>";
}

else {
$six = 'ff';
}

##########################################################
##########################################################
######################
## Final evaluation ##
######################
if (($one eq 'aa') && ($two eq 'bb') && ($three eq 'cc') && ($four eq 'dd') && ($five eq 'ee') && ($six eq 'ff')) {
print "<p><font face=arial size=2><P><font color=green><b>Form Completed Correctly</b></font>";
}

else {
print "<p><font face=arial size=2><P><font color=red><b>Form Completed Incorrectly</b></font>";

}
##########################################################
#########
## End ##
#########
 
I just moved the print "Content-type: text/html\n\n";
as you said, and it worked... Thank you for your help. PROBLEM SOLVED
 
No problem.

Just so you know the reason to change eq to == is that eq is a string comparison and == is a numerical comparison. It will work sometimes if you do it wrong.. but it will come back to haunt you :)


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top