Forum Moderators: coopster
For example, I've set up my forms so they can't be submitted with certain or no data. However, how do I set it so they can't be submitted if they contain certain characters or vise versa.
For example:
Say the user submitted a URL instead of an email, then there would be an error because there was no '@' character used.
<?
$email = trim($_POST['email']);
$myemail = "***@***.co.uk";
$pos = strpos($email "@");
if($pos === false)
{ echo "<h1>Error</h1><br>..."; }
elseif($pos > 1)
{ echo "<h1>Error</h1><br>..."; }
elseif($email == "your-email@domain.com")
{ echo "<h1>Error</h1><br>..."; }
elseif($email == "")
{ echo "<h1>Error</h1><br>..."; }
else
{
mail($myemail, "Mailing List", "Email: $email");
echo "<h1>Thank You</h1><br>...";
}
?>
Here are all the alternatives that I tried:
$pos = ($email "@");
$pos = ("$email" "@");
$pos = $email "@";
$pos = "$email" "@");
$pos = strpos($email "@");
$pos = strpos("$email" "@");
$pos = ($email, "@");
$pos = ("$email", "@");
$pos = $email, "@";
$pos = "$email", "@");
$pos = strpos($email, "@");
$pos = strpos("$email", "@");
$isEmail = '/^[^@\s<&>]+@([-a-z0-9]+\.)+[a-z]{2,}$/i';
if(preg_match($isEmail, $email)){
it's an email
}else{
it's not
}
regular expression can be very helpful and I had a hard time getting my head round it at the very begining. I'll try to explain the above $isEmail (reg expression used by C.Shiflett in his book):
/^: starts regular expression
[^@\s<&>]: you cannot have (^) a @ or a space (\s) or < or & or > in the first part of the email [ ]
+@: you have to have a @
([-a-z0-9]: then a combination of letters (lowercases) and/or numbers with possibly hyphens (the first -)
+\.: then a . (the . has to be escaped with \
+[a-z]{2,} and finally at least 2 lowecase letters
$/i: ends regular expression
[edited by: le_gber at 3:06 pm (utc) on July 13, 2006]
This line of code would not be needed if you use the solution that myself or le_gber posted.