Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Problems? Is your data what you think it is?
 
PerlMonks  

Allow empty form fields regex

by Hissingsid (Sexton)
on Mar 18, 2004 at 15:36 UTC ( #337704=perlquestion: print w/ replies, xml ) Need Help??
Hissingsid has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I'm using a regex to allow input of some HTML markup into a form field but the field is still acceptable if it is empty. Here is what I'm using which seems to be validating in the way I want it to except if the field is empty then I get a false.

if ($contents != /^[\w \.\,\=\" \/<>]+$/ ){ $contentserror = "Error - message.";++$error; }

My question is: Can you please give me an example of how I can allow the field to be empty but only validate against the regex above if it has any characters in it.

I guess I could hack it by saying this first

if ($contents = ""){ $contents = "\s"; }

and then put it through the regex but that is a real mess.

Any ideas, I've done a few searches but can't find anything to help me out there.

Many thanks
Sid

20040318 Edit by Corion: Added code tags

Comment on Allow empty form fields regex
Select or Download Code
Re: Allow empty form fields regex
by tcf22 (Priest) on Mar 18, 2004 at 16:07 UTC
    How about this
    if ((length($contents) > 0) && ($contents !~ /^[\w \.\,\=\" \/<>]+$/)) +{ $contentserror = "Error - message.";++$error; }
    Also != should probably be !~.

    - Tom

Re: Allow empty form fields regex
by Paladin (Priest) on Mar 18, 2004 at 16:08 UTC

    First, the operator to bind a regex match to a variable is =~ and to negate that, it's !~

    Second, the operator to compare strings is eq. = is the assignment operator.

    Now that that's out of the way, you could do something like:

    if ( ($contents ne "") && ($contents !~ /^[\w \.\,\=\" \/<>]+$/) ){ $contentserror = "Error - message.";++$error; }
    This will first check if $contents is not empty, and if it's not, then check it against the regex, and if the regex doesn't match, then execute the body of the if statement.
Re: Allow empty form fields regex
by matija (Priest) on Mar 18, 2004 at 16:12 UTC
    Well, at first I wanted to say you only need to add a |(^$) to your regexp, but some experimentation has showed me that doesn't work.

    Then I took a good look at you regexp. You are accepting any of a class of characters, one or more times. To accept an empty field as well, simply change the + to *:

    if ($contents != /^[\w \.\,\=\" \/<>]*$/ ){ $contentserror = "Error - message.";++$error; }
    It's not a general answer, but in your particular case, it should work.
      Hi,

      Thanks for the excellent input.

      I played around a bit and this is what I have got working perfectly.

      if (($contents ne "") && (!($contents =~ /^[\w \.\,\=\" \/<>]+$/))){ $contentserror = "Error - message.";++$error; }
      For some reason this != was not helping the situation.

      Anyway Paladin, a big thanks I had been fiddleing for hours and now have a good solution thanks to your suggestion.

      Best wishes

      Sid

      Edit by castaway, added code tags

        Apologies guys I missed the bit about !=/!~

        !~ does indeed work removing the need for some of the ()s.

        Many thanks Sid

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://337704]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (18)
As of 2014-04-17 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (449 votes), past polls