Regex Email

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
mc_guy
Forum Newbie
Posts: 5
Joined: Tue Mar 27, 2012 11:07 pm

Regex Email

Post by mc_guy »

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
============

Code: Select all

<?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.
Reason: Moved to Regex forum by moderator, and changed Subject.
User avatar
ragax
Forum Commoner
Posts: 85
Joined: Thu Dec 15, 2011 1:40 pm
Location: Nelson, NZ

Re: Regex Email

Post by ragax »

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. :)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Regex Email

Post by requinix »

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.
User avatar
ragax
Forum Commoner
Posts: 85
Joined: Thu Dec 15, 2011 1:40 pm
Location: Nelson, NZ

Re: Regex Email

Post by ragax »

That regex of yours is a brilliant work of art, requinix!
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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Regex Email

Post by requinix »

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 :(
mc_guy
Forum Newbie
Posts: 5
Joined: Tue Mar 27, 2012 11:07 pm

Re: Regex Email

Post by mc_guy »

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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Regex Email

Post by requinix »

You are actually calling verify_Email() somewhere, right?
mc_guy
Forum Newbie
Posts: 5
Joined: Tue Mar 27, 2012 11:07 pm

Re: Regex Email

Post by mc_guy »

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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Regex Email

Post by Celauran »

Not regex, but I'll just leave this here: PHP Validate filters.
User avatar
ragax
Forum Commoner
Posts: 85
Joined: Thu Dec 15, 2011 1:40 pm
Location: Nelson, NZ

Re: Regex Email

Post by ragax »

[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:

Code: Select all

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.
mc_guy
Forum Newbie
Posts: 5
Joined: Tue Mar 27, 2012 11:07 pm

Re: Regex Email

Post by mc_guy »

Ragax.. thanks for the tip. Will get back if I have any questions!
Post Reply