DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   PHP (http://www.daniweb.com/forums/forum17.html)
-   -   preg match (http://www.daniweb.com/forums/thread161929.html)

designingamy Dec 12th, 2008 9:41 am
preg match
 
Hello all,
I am having a hard time trying to figure out why I can't get my code to successfully check my email address. It seems I can plug in anything and it won't give me an error.

This is my code for that line:

<?php
session_start();
if((!empty($_POST['email'])
&& (preg_match('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $_POST['email']) <50)))
{
$email=$_POST['email'];
$email=htmlspecialchars($email);
$_SESSION['email'] = $email;
}
else
{
$errors[]= 'You forgot to enter a valid E-mail Address!';
}

if (empty($errors))
{
header
("Location:http://www.thisnextpage.php");
exit();
}

if (!empty($errors) && is_array($errors))
{
echo '<h1>Error!</h1>
The following error(s) occured:<br/ >';
foreach ($errors as $msg)
{
echo " - $msg<br />\n";
}
}

?>

When I type in just letters and don't include @ it accepts it and moves me right along. It's supposed to stop me and tell me to enter a valid email address. Hmmmm...

Any help will be appreciated :)
~Amy

kkeith29 Dec 12th, 2008 11:46 am
Re: preg match
 
why are comparing the preg_match to "<50". preg_match returns true or false. it will return a matches array that you can get the count of if you are trying to make sure there are not over 50 email addresses in the field, which is probably not what you are trying to do.

just remove that and it should work. i didn't check out the rest of the code though, so I can't tell for sure.

sikka_varun Dec 12th, 2008 11:47 am
Re: preg match
 
Hi...
Why are u comparing this value with 50....
preg_match returns true of false...
If you are comparing with the strlength, then use different if for comparing the string length..

use the following if condition..
this will run...

if( !empty($_POST['email'])  && preg_match('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $_POST['email']) )

designingamy Dec 12th, 2008 12:05 pm
Re: preg match
 
Oh, I see. I was trying to control how many characters were entered in the box. Silly me. Thanks everyone! It works now!
~Amy


All times are GMT -4. The time now is 10:18 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC