preg match

Thread Solved
Reply

Join Date: Aug 2008
Posts: 94
Reputation: designingamy is an unknown quantity at this point 
Solved Threads: 0
designingamy designingamy is offline Offline
Junior Poster in Training

preg match

 
0
  #1
Dec 12th, 2008
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:

  1. <?php
  2. session_start();
  3. if((!empty($_POST['email'])
  4. && (preg_match('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $_POST['email']) <50)))
  5. {
  6. $email=$_POST['email'];
  7. $email=htmlspecialchars($email);
  8. $_SESSION['email'] = $email;
  9. }
  10. else
  11. {
  12. $errors[]= 'You forgot to enter a valid E-mail Address!';
  13. }
  14.  
  15. if (empty($errors))
  16. {
  17. header
  18. ("Location:http://www.thisnextpage.php");
  19. exit();
  20. }
  21.  
  22. if (!empty($errors) && is_array($errors))
  23. {
  24. echo '<h1>Error!</h1>
  25. The following error(s) occured:<br/ >';
  26. foreach ($errors as $msg)
  27. {
  28. echo " - $msg<br />\n";
  29. }
  30. }
  31.  
  32. ?>

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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,225
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 166
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: preg match

 
0
  #2
Dec 12th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 94
Reputation: sikka_varun is an unknown quantity at this point 
Solved Threads: 11
sikka_varun's Avatar
sikka_varun sikka_varun is offline Offline
Junior Poster in Training

Re: preg match

 
0
  #3
Dec 12th, 2008
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...

  1. if( !empty($_POST['email']) && preg_match('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $_POST['email']) )
VâRûN
---Happy to Help---
sikka_varun@yahoo.com
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 94
Reputation: designingamy is an unknown quantity at this point 
Solved Threads: 0
designingamy designingamy is offline Offline
Junior Poster in Training

Re: preg match

 
0
  #4
Dec 12th, 2008
Oh, I see. I was trying to control how many characters were entered in the box. Silly me. Thanks everyone! It works now!
~Amy
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC