PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Nov 16, 2018 9:55 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Regex Email
PostPosted: Tue Mar 27, 2012 11:34 pm 
Offline
Forum Newbie

Joined: Tue Mar 27, 2012 11:07 pm
Posts: 5
Guys... this is my first post in this forum. I hope am at the right place!
Note: I did scripting in perl/shell.. but not in Php.. So trying to understand a little bit more. As you can understand thats why I'm here.... :D

I have a website in html for which I have a 'contact us' page, which contains email id field. Just before submitting.. am using this quick and simple PHP regular expression for quick email validation. But apparently this check is NOT working :roll: (even though the contact info is successfully sent) .. Wondering why?

Any help is much appreciated.

This is the php code am using
============


Syntax: [ Download ] [ Hide ]
<?php

$SendFormTo = 'sales@mycomp.com';
$Emailsubject = 'Contact us email from mycomp.com';

$Name = $_POST['Name'];

$Email = $_POST['Email'];

function verify_Email($Email)
{
  if(!preg_match('/^[_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*@[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,4})$/',$Email))
  {
    return false;
   
  } else
  {
   return $Email;
  }
}

$Comments = $_POST['Comments'];

$body = "

Name - $Name
Email - $Email
Comments - $Comments

"
;

$headers = "From: $Email";
$success = mail($SendFormTo, $Emailsubject, $body, $headers);

header( "Location: http://www.mycomp.com" );

?>


Last edited by Christopher on Fri Mar 30, 2012 3:17 pm, edited 2 times in total.
Moved to Regex forum by moderator, and changed Subject.


Top
 Profile  
 
 Post subject: Re: Regex Email
PostPosted: Fri Mar 30, 2012 3:29 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
Hi mc,

First, a bit of feedback. The reason I haven't responded yet (and I'm pretty sure several other contributors would feel the same) is that your question is too vague. "Is not working" is just way too broad and puts an extra load of work on the people who respond to try to understand what might not be working for you.

When you ask a regex question, can you please describe exactly the desired input and output (or faulty behavior)?

Now, taking a quick look:
- A-z is incorrect syntax. You can either say [A-Za-z] or [[:alpha:]] or [a-z] in case-insensitive mode.
---> Where you have [_A-z0-9-], I would write [\w\d-] or [[:alnum:]_-]
---> Where you have [A-z]{2,4}, I would write [[:alpha:]]{2,4}
- I'm a bit surprised by the plus (+) in (\.|\+), this means you could have joe.mama+papa@family.com, but if that's what you want, no worries. However, since you're only matching a single character, replace that (\.|\+) with [.+], you can insert whatever other special delimiter you want to allow in the brackets

Let me know if you have questions about this or want to take it further. :)


Top
 Profile  
 
 Post subject: Re: Regex Email
PostPosted: Fri Mar 30, 2012 4:39 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
Did I hear someone ask for a regex to validate an email address?

(Kinda "ask". Close enough to asking for me.)

And [A-z] is valid. Allows for [ \ ] ^ _ ` but only the last three of those are actually okay.


Top
 Profile  
 
 Post subject: Re: Regex Email
PostPosted: Fri Mar 30, 2012 6:02 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
That regex of yours is a brilliant work of art, requinix!

Quote:
And [A-z] is valid. Allows for [ \ ] ^ _ ` but only the last three of those are actually okay.

Thank you for pointing that out. @mc, to clarify, [A-z] does define a range of characters, just not exactly the same range as the [A-Za-z] (same as [[:alpha:]] ) that I assumed you meant.


Top
 Profile  
 
 Post subject: Re: Regex Email
PostPosted: Fri Mar 30, 2012 8:49 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
ragax wrote:
Thank you for pointing that out.

Don't thank me. I was using it as an excuse for mentioning that huge regex, even though it wasn't what mc_guy actually asked about :(


Top
 Profile  
 
 Post subject: Re: Regex Email
PostPosted: Sat Mar 31, 2012 1:18 am 
Offline
Forum Newbie

Joined: Tue Mar 27, 2012 11:07 pm
Posts: 5
Guys.. thanks for the replies and pointing out the flaw. Infact I just picked up this part of code from
http://www.soaptray.com/blog/2008/04/va ... using-php/ as I just started to learn php.

Ragax: Am surprised when you said my question is too vague. Anyway let me rewrite my question once again.
I'm able to get the email with Name,email and comments in it but unfortunately the email check is just NOT working which means to say even if you type a garbage in the email field it still takes and sends thru instead of restricting the same. Hope you got what I'm saying.

Thanking you in advance.


Top
 Profile  
 
 Post subject: Re: Regex Email
PostPosted: Sat Mar 31, 2012 1:41 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
You are actually calling verify_Email() somewhere, right?


Top
 Profile  
 
 Post subject: Re: Regex Email
PostPosted: Sat Mar 31, 2012 2:07 pm 
Offline
Forum Newbie

Joined: Tue Mar 27, 2012 11:07 pm
Posts: 5
Reqquinix, thanks for asking this.

Unfortunately NO. I'm calling this php program (that includes this function) from the html form.

==============
</form>
<form id="f8" action="/mcformmailer.php" method="post" onsubmit="return weCheckForm(this)">
<fieldset id="e8" class="form_set">
<legend id="e7" class="form_label">
Name
</legend><br>
<input id="e6" class="form_input" type="text" name="Name" title="Name" size="37"><br>
<label id="e5" class="form_label">
E-mail
</label><br>
<input id="e4" class="form_input" type="text" name="Email" title="E-mail" size="37"><br>
<label id="e3" class="form_label">
Comments
</label><br>
<textarea id="e2" class="form_input" name="Comments" title="Comments" rows="3" cols="29">
Enter Comments here...
</textarea><br>
<input id="e1" class="form_button" type="submit" title="Send Details" value="Send Details">
</fieldset>
</form>

==========

Frankly speaking I was not thinking about calling this function explicitly from the html. Whether I really need to?

If yes, I don't want to make a fool of myself here asking some basic questions like how to call a php function from html script. Let me read thru / learn as how to call a php function.

Thanks again for all the help you guys provided so far. Much appreciated.


Top
 Profile  
 
 Post subject: Re: Regex Email
PostPosted: Sat Mar 31, 2012 2:31 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6424
Location: Montreal, Canada
Not regex, but I'll just leave this here: PHP Validate filters.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
 Post subject: Re: Regex Email
PostPosted: Sat Mar 31, 2012 2:34 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
[EDIT: Celauran's reply and mine crossed paths, I didn't mean to be rude by now acknowledging it.]

Wow, requinix, that's a great catch. Are you a bit psychic, or do you just know how to read? :lol:

@mc_guy, I'd suggest not worrying too much about "silly questions"... There's always someone who knows more than you, I often feel silly but there's a lot of support for learning on the forums. :mrgreen:

To clean up your code, your function can move somewhere near the bottom of the script.
Then somewhere in the script, where you decide whether to send the email, you write something like:

Syntax: [ Download ] [ Hide ]
if(verify_Email($Email))
  {
  // what you do if the email is valid. Probably this is where you paste your code to send the email.
  // the reason an IF works is that the function returns "false" when it cannot validate an email. To clean up, you could probably stand to make it return 1 or true if it's happy with the email.
  }
else
 {
  // what do you want to do something if the email is not valid?
  // if you want the form to alert the user, then you will have to reload the page.
  // some people would prefer to validate the email ON the page, in javascript, rather than to cause the page to refresh.
 }
 


I hope I haven't sent you down the wrong track, if so a coding guru will surely let you know.
Wishing you a fun weekend.


Top
 Profile  
 
 Post subject: Re: Regex Email
PostPosted: Mon Apr 02, 2012 12:44 am 
Offline
Forum Newbie

Joined: Tue Mar 27, 2012 11:07 pm
Posts: 5
Ragax.. thanks for the tip. Will get back if I have any questions!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group