View Full Version : Regular Expressions help

07-12-2007, 07:18 AM
I have a form that validates each field before it can be submitted using regular expressions. I also have an unordered list that returns different error messages based on what the regular expressions evaluate to. What I would like to do is create a different error message when a field is blank vs. when it contains invalid characters. Here is the relevant code that I have right now:


if (!eregi("^[[:alpha:]]+$", $firstName)) {$firstNameError=1}

What I thought I should do is create an if statement that reads:

if ($firstName=="") {create separate error message}

However that will not work since the first if statement also tests for at least one character. I could create a regular expression that tests for the presence of every character other than letters, but that seems like a ridiculous thing to do. Any help is appreciated.

07-12-2007, 04:36 PM
Check for blanks before your regex.

if(blank condition)

elseif(regex condition)


If the blank condition returns true the regex won't ever be run.

Good luck;

07-12-2007, 04:42 PM
good way to check if blank is either of:

$var == ""

Hope that helps you with the above.

07-13-2007, 12:56 AM
Why didn't I think of that? Thanks for your help.