Check Email function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Check Email function

Post 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
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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 '!'
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post by hairyjim »

Cheers got it working.

Couldn't see the wood for the trees.

Jim
Post Reply