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!
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
// 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;
}
}
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.
// 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;
}
}
}