Re: Checking for empty CGI params; this isn't working
by mrbbking (Hermit) on Jul 14, 2002 at 17:28 UTC
|
You're checking the length of the result of an 'or' between two calls to the param() sub. There is no context in your post, but I don't think that's what you want. Look closely at how your parentheses match up:
(param('tele') || param('mobile'))
Do you want, instead, to check that one (or both) of the params "mobile" and "tele" are at least one character long?
If so, try this:
elsif( length(param('tele')) || length(param('mobile')) )
If not, please ask again, but be clear in your question what it is that you're looking for, and exactly how the code you have so far is not doing what you want. (For more on this, have a look at How do I post a question effectively?) It is difficult to tell what you're trying to ask, here.
Welcome to the Monastery, andrew - I hope you find what you're looking for here. | [reply] [d/l] [select] |
|
I want to check to see if either telephone or mobile is entered, only one has to be entered to get through and your last post did not work
elsif( length(param('tele')) || length(param('mobile')) ) {
error("You need to enter either a telephone or a mobile");
}
| [reply] [d/l] |
|
my $tele = '';
my $mobile = '';
# uncomment to pass
# $tele = 'hello';
if( length($tele) || length($mobile) ){
print 'At least one is OK';
}
else {
print 'both are empty';
}
| [reply] [d/l] |
Re: Checking for empty CGI params; this isn't working
by particle (Vicar) on Jul 14, 2002 at 16:56 UTC
|
elsif( defined( param('tele') || param('mobile') ) ) {
Update: hrmph. length should work just as well.
elsif( length( param('tele') || param('mobile') ) ) {
works for me.
~Particle *accelerates*
| [reply] [d/l] [select] |
|
Even if I enter a tele it still gives me the error
| [reply] |
|
perhaps you mean $q->param() ? i don't know anything about your param sub, so i can't comment further. if it's an object method, you left off the object.
~Particle *accelerates*
| [reply] [d/l] [select] |
Re: Checking for empty CGI params; this isn't working
by astaines (Curate) on Jul 14, 2002 at 17:56 UTC
|
You're checking the truth-value of the 'length' of an 'or' of two calls to 'param()' - this is probably not what you meant (-;
elsif (
(length(param('tele') > 0)
or
(length(param('mobile') > 0)
)
might be closer to your intentions. Any views on optimum indentation style for code readability?
--
Anthony Staines | [reply] [d/l] |
|
syntax error at /var/securewww/virtual/webewebin.com/a/signup.cgi line
+ 61, near ") {"
syntax error at /var/securewww/virtual/webewebin.com/a/signup.cgi line
+ 76, near "}"
(Might be a runaway multi-line ~~ string starting on line 73)
Execution of /var/securewww/virtual/webewebin.com/a/signup.cgi aborted
+ due to compilation errors.
I get that error from
elsif ((length(param('tele') > 0) || (length(param('mobile') > 0)) {
error("You need to enter either a telephone or a mobile");
}
</code> | [reply] [d/l] [select] |
|
elsif (
(length(param('tele')) > 0)
||
(length(param('mobile')) > 0)
) { .. }
you've simply missed a brace each after 'tele' and 'mobile' :-)
Have a nice day
All decision is left to your taste
| [reply] [d/l] |
|
|
|
Re: Checking for empty CGI params; this isn't working
by stajich (Chaplain) on Jul 14, 2002 at 23:53 UTC
|
Can't do it this way. The || will cast the string returned from param to a boolean. As other monks have pointed out do
if( (length(param('tele')) > 0) ||
(length(param('mobile')) > 0) ) {
}
However your logic doesn't match your error message - if you want to verify that the user has entered only one of the above you'll want to use && and if you want to make sure that they have entered at least one you'll want to warn when both tele and mobile are empty, i.e. length = 0 (although as pointed out before I would use defined to make sure the values aren't null).
You probably want to catch that just whitespace isn't being returned too... | [reply] [d/l] [select] |