in reply to Allow empty form fields regex

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.

Replies are listed 'Best First'.
Re: Re: Allow empty form fields regex
by Hissingsid (Sexton) on Mar 18, 2004 at 16:55 UTC
    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