Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Checking for empty CGI params; this isn't working

by andrew (Acolyte)
on Jul 14, 2002 at 16:50 UTC ( #181592=perlquestion: print w/replies, xml ) Need Help??
andrew has asked for the wisdom of the Perl Monks concerning the following question:

elsif(length(param('tele') || param('mobile')) > 0) { error("You need to enter either a telephone or a mobile"); }
Ahh what's wrong

Edited: Mon Jul 15 04:12:05 2002
by footpad: Updated title (in entire thread), per Consideration.

Replies are listed 'Best First'.
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.

      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"); }
        I don't write a lot of CGI programs, so I'm not going to be an authority on use of the param() function -- but it may be complicating that line more than necessary. I'm guessing that you call it on these two fields at least twice - once (here) to see if its empty, and again later to use it.

        You may want to take that out of the question by assigning each to a scalar value. Then this works:

        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'; }
Re: Checking for empty CGI params; this isn't working
by particle (Vicar) on Jul 14, 2002 at 16:56 UTC
    better to use defined rather than length here.

    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*

      Even if I enter a tele it still gives me the error
        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*

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
      syntax error at /var/securewww/virtual/ line + 61, near ") {" syntax error at /var/securewww/virtual/ line + 76, near "}" (Might be a runaway multi-line ~~ string starting on line 73) Execution of /var/securewww/virtual/ 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"); }

        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

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...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://181592]
Approved by particle
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2018-02-22 11:16 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (291 votes). Check out past polls.