Page 1 of 1

Check Email function

Posted: Fri May 14, 2004 6:22 am
by hairyjim
Hi All,

I'm trying to go through some basics of classes and functions and I am attempting to modify an existing function (1st code block below) so that it checks to see if there is any data in the field before performing validation. If it does then validate otherwise exit and return true. My attempt is the 2nd code block.

Code: Select all

function isEmailAddress($field, $msg)
	{
		$value = $this->_getValue($field);
		$pattern = "/^(їa-zA-Z0-9])+(ї\.a-zA-Z0-9_-])*@(їa-zA-Z0-9_-])+(\.їa-zA-Z0-9_-]+)+/";
		if(preg_match($pattern, $value))
		{
			return true;
		}
		else
		{
			$this->_errorListї] = array("field" => $field, "value" => $value, "msg" => $msg);
			return false;
		}
	}
This is what I thought would work:

Code: Select all

// check whether email field has data in or not, if it does then validate
	function isEmailOrEmpty($field, $msg)
	{
		//$value = $this->_getValue($field);
		if(!isset($this->_getValueї$field]))
		{
			$pattern = "/^(їa-zA-Z0-9])+(ї\.a-zA-Z0-9_-])*@(їa-zA-Z0-9_-])+(\.їa-zA-Z0-9_-]+)+/";
			if(preg_match($pattern, $value))
			{
				return true;
			}
		}
		else
		{
			return true;
		}
	}
Now this doesn't seem to work. Could someone offer me some words of advice.

Regards
James

Posted: Fri May 14, 2004 6:26 am
by kettle_drum
if(!isset($this->_getValue[$field])) - this only runs the loop to check the pattern of the email if the value is not set. SO at the moment your returning true only if the value is set - and it doesnt matter if its valid or not.

Just remove the '!'

Posted: Fri May 14, 2004 7:43 am
by hairyjim
Cheers got it working.

Couldn't see the wood for the trees.

Jim