...

View Full Version : Regular Expressions help



zach4618
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:


$firstNameError=0;

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.

Brandoe85
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;

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

is_empty()
isset()
$var == ""

Hope that helps you with the above.

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum