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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If statement help

Status
Not open for further replies.

shaolinf

Programmer
Nov 20, 2007
14
GB
Hi Guys,

Im making a form and when I process it and remove the "&" and "=" chars I want to check if the user entered any data. Here is the if statement:

if ($formdata{'name'} == "" || $formdata{'username'} == "" || $formdata{'password'} == "" || $formdata{'date'} == "") {
print "No data entered!";
}

It works, but the statement is too long. Are there any shorter ways of expressing the same conditions ?
 
Why do you feel it is to long? There's nothing wrong with that other than you should probably be doing eq instead of ==. You can also probably do
f ($formdata{'name'} && $formdata{'username'} && $formdata{'password'} && $formdata{'date'} )

because if they enter nothing those should exists. YOu also talk about getting rid of & and ='s.. are you using CGI, if not I would recommended.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[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;
 
Code:
my @form_fields = qw(name username password date);
foreach my $field (@form_fields) {
   print "No data entered for $field!<br>\n" unless $formdata{$field};
}

But you probably should validate each field seperately anyway.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
As Kevin said, you should probably validate each field separately so that you can output context sensitive error messages. However, what you're ultimately looking for is grep.

Code:
[olive][b]if[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/grep.html][black][b]grep[/b][/black][/url] [red]{[/red][blue]$formdata[/blue][red]{[/red][blue]$_[/blue][red]}[/red] eq [red]'[/red][purple][/purple][red]'[/red][red]}[/red] [red]qw([/red][purple]name username password date[/purple][red])[/red][red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]No data entered![/purple][red]"[/red][red];[/red]
[red]}[/red]

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top