Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: HTML PHP
function checkEmail ($_POST['email'])
	{
	$possible = "/^[a-z]$/i";
	if ($_POST['email'] >= 8)
		{
		if (preg_match ($possible, $_POST['email']))
			{
			return true;
			}
		else
			{
			return false;
			}
		}
	else 
		{
		return false;
		}
	}
 
I just get the following Error: Fatal error: Cannot re-assign auto-global variable $ _POST in /homepages/... in line 1
If I dont use the part "$_POST['email']" in "function checkEmail ()" it always return false if i have 8 or more chars from a to z but it should return true.
Where is my mistake? And how can I add @ and . to $possible?
Posted 8-Sep-12 9:13am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Simply put, you shouldn't be using $_POST in the function signature(or body) - you should be using it in the place that you call your function.
 
function checkEmail($emailAdr)
{
    $possible = "/^[a-z]$/i";
 
/* unsure about this line, your intention is unclear - i think you want the email address to be at least 8 characters. */
    if (strlen($emailAdr) >= 8)
    {
        if (preg_match ($possible, $emailAdr))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else 
    {
        return false;
    }
}
 
Followed by the call to the function:
$emailValid = checkEmail($_POST['email']);
  Permalink  
Comments
Chi Ller at 9-Sep-12 4:44am
   
Now it is a form and i get all entries with a $_POST variable. So how can I get the content of $_POST['email'] for do the function with the variable $email?
 
Yes you´re right. I want tho check the email to be 8 characters
enhzflep at 9-Sep-12 8:58am
   
Okay sure. Er, ahrm - have a closer look at my previous post. Perhaps this will be more explicit:
 
$enteredEmail = $_POST['email'];
$emailValid = checkEmail($enteredEmail);
Chi Ller at 9-Sep-12 11:30am
   
Yes but the next problem is preg_match. Cause of it I always get false.
for example:
$enteredEmail is testmailhere (12 letters, just letters from a-z) and preg_match returns false
enhzflep at 9-Sep-12 11:53am
   
Yes I see that, the problem is with the regex you're using. Simply put, it's just wrong. I got put onto Expresso by OriginalGriff a while ago for working with RegExps - it's well worth the download.
 
I just grabbed the expression for email validation from it, it a _little_ longer than yours was. :grin:
 
Just drop this line into your code and it will work:
 

$possible = "/([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})/";
Chi Ller at 9-Sep-12 12:18pm
   
sorry but it also doesn´t work :S
enhzflep at 9-Sep-12 12:27pm
   
Well, of course it won't work if you're using an email address of 'testmail' - that address IS invalid...
 
Use a real email address, then accept the answer when you realize it does work. :)
 
EDIT: Try this:
<?php
function checkEmail($emailAdr)
{
$possible = "/([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})/";
 
if ((strlen($emailAdr) >= 8)&&(preg_match ($mRegex, $emailAdr)))
return true;
return false;
}
 
if (checkEmail("enhzflep@yahoo.com.au"))
echo "enhzflep@yahoo.com.au DOES pass";
else
echo "enhzflep@yahoo.com.au does NOT pass";
?>
Chi Ller at 9-Sep-12 13:32pm
   
ouh sorry I forgot that this code checks also @ and .
enhzflep at 9-Sep-12 13:38pm
   
:) It happens..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 8 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid