Forum Moderators: coopster

Message Too Old, No Replies

Allow blank fields

I want to allow a user to leave fields blank.

         

max4

5:23 pm on May 8, 2009 (gmt 0)

10+ Year Member



Hello,

After many searches, it would seem that most threads are about returning errors if a field is left blank - but I have no difficulty doing that. The problem arises when I want a user to have the option of leaving fields blank. This is what we are looking at:

if (!preg_match("/^([0-9])+$/",$icq)) {
$invalidIcq = 1;
header("Location: example.php?invalidIcq=1");
}
elseif (!preg_match("/^([a-z0-9])+$/",$aim)) {
$invalidAim = 1;
header("Location: example.php?invalidAim=1");
}
else
{
Do something
}

Now, a user might have an ICQ number but not an AIM ID, so they may wish to enter one or the other. But if one of the fields is left blank the invalid error is returned. I think this is happening because the code is checking against the preg_match function and an empty field is not a character in the regex. I'm completely baffled by this; any ideas on how to allow a user to leave a field blank while still performing a preg_match function?

Thank you,
Max

henry0

5:53 pm on May 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might use if (isset() )
isset() [php.net]
$$ if(!empty() )
empty() [php.net]

then adapt your check logic
if a POST is !empty and is set check it out
else do nothing

max4

7:27 pm on May 8, 2009 (gmt 0)

10+ Year Member



Thank you Henry, your suggestion worked. !empty() &&. ...