Email validate 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

Email validate function

Post by hairyjim »

Hi all,

The following is my function for checking if an email address is in the correct format. Basically I want the function to only check if the email address field has been entered into or not, if it has then check validation if not don't bother trying to validate.

I just can't get it too work. Regardless of what I enter into the field the form will process, I know the form is ok and the call to the class is ok because I have other functions being tested also and they work.

Could someone please point me towards the direction of divine enlightenment :)

Code: Select all

// check whether email field has data in or not, if it does then validate 
   function isEmailOrEmpty($field, $msg) 
   { 
      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 false; 
      } 
   }
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

where's $value coming from?
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post by hairyjim »

Code: Select all

// function to get the value of a variable (field)
	function _getValue($field) 
    { 
        extract($_REQUEST); 
        return ${$field}; 
    }
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you don't save the return anywhere.
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post by hairyjim »

Woahhh hang on. You have hit a nail on the head there me thinks.

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

Post by hairyjim »

Finally fixed it.

2 things I did wrong. I did not return the error and also dod not set $value.

Stupid mistakes considering i have just done similar things for several other validation functions.

One thing I can't figure out though is if I use if(isset($value)) the validation does not work, but if I change the line to if (!trim($value) == "") it does work.

Id be interested in knowing why.

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($value))
		if (!trim($value) == "") 
		{
			$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;
			}
		}
	}
Post Reply