Page 1 of 1

Email validate function

Posted: Mon Sep 06, 2004 6:06 am
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; 
      } 
   }

Posted: Mon Sep 06, 2004 6:11 am
by feyd
where's $value coming from?

Posted: Mon Sep 06, 2004 6:21 am
by hairyjim

Code: Select all

// function to get the value of a variable (field)
	function _getValue($field) 
    { 
        extract($_REQUEST); 
        return ${$field}; 
    }

Posted: Mon Sep 06, 2004 6:22 am
by feyd
you don't save the return anywhere.

Posted: Mon Sep 06, 2004 6:23 am
by hairyjim
Woahhh hang on. You have hit a nail on the head there me thinks.

Jim

Posted: Mon Sep 06, 2004 6:59 am
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;
			}
		}
	}