Page 1 of 1
Regex Email
Posted: Tue Mar 27, 2012 11:34 pm
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....
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

(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" );
?>
Re: Regex Email
Posted: Fri Mar 30, 2012 3:29 pm
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.

Re: Regex Email
Posted: Fri Mar 30, 2012 4:39 pm
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.
Re: Regex Email
Posted: Fri Mar 30, 2012 6:02 pm
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.
Re: Regex Email
Posted: Fri Mar 30, 2012 8:49 pm
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

Re: Regex Email
Posted: Sat Mar 31, 2012 1:18 am
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.
Re: Regex Email
Posted: Sat Mar 31, 2012 1:41 am
by requinix
You are actually calling verify_Email() somewhere, right?
Re: Regex Email
Posted: Sat Mar 31, 2012 2:07 pm
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.
Re: Regex Email
Posted: Sat Mar 31, 2012 2:31 pm
by Celauran
Not regex, but I'll just leave this here:
PHP Validate filters.
Re: Regex Email
Posted: Sat Mar 31, 2012 2:34 pm
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?
@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.
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.
Re: Regex Email
Posted: Mon Apr 02, 2012 12:44 am
by mc_guy
Ragax.. thanks for the tip. Will get back if I have any questions!