Bluehost.com Web Hosting $6.95

preg question

This is a discussion on preg question within the PHP General forums, part of the PHP Programming Forums category; Hi all, I am trying to do a simple validation of an email address being submitted. I have the @ sign ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-13-2003
Jake McHenry
 
Posts: n/a
Default preg question

Hi all,

I am trying to do a simple validation of an email address being
submitted. I have the @ sign being validated, but I can't get the
period to work.. Can someone help me out?

Here's my code..

if ($_POST['Travel_Request_Email_Address'] != "")
{
if (preg_match_all("/(@)/",
$_POST['Travel_Request_Email_Address'], $match) != 1)
{
$errorcount++;
$error = $error . "    " . $errorcount .")
<font color=\"red\">Email Address</font> field does not contain @<br>
\n";
}
else if (preg_match_all("/(.)/",
$_POST['Travel_Request_Email_Address'], $match) < 1)
{
$errorcount++;
$error = $error . "&nbsp;&nbsp;&nbsp;&nbsp;" . $errorcount .")
<font color=\"red\">Email Address</font> field does not contain .<br>
\n";
}
else
{
$_SESSION['Travel_Request_Email_Address'] =
$_POST['Travel_Request_Email_Address'];
}
}
else
{
$errorcount++;
$error = $error . "&nbsp;&nbsp;&nbsp;&nbsp;" . $errorcount .")
<font color=\"red\">Email Address</font> field is empty<br>\n";
}

Thanks,

Jake McHenry
Nittany Travel MIS Coordinator
http://www.nittanytravel.com
Reply With Quote
  #2 (permalink)  
Old 11-13-2003
Jake McHenry
 
Posts: n/a
Default RE: [PHP] preg question

> -----Original Message-----
> From: Jake McHenry [mailto:linux@nittanytravel.com]
> Sent: Thursday, November 13, 2003 2:50 PM
> To: php-general@lists.php.net
> Subject: [php] preg question
>
>
> Hi all,
>
> I am trying to do a simple validation of an email address
> being submitted. I have the @ sign being validated, but I
> can't get the period to work.. Can someone help me out?
>
> Here's my code..
>
> if ($_POST['Travel_Request_Email_Address'] != "")
> {
> if (preg_match_all("/(@)/",
> $_POST['Travel_Request_Email_Address'], $match) != 1)
> {
> $errorcount++;
> $error = $error . "&nbsp;&nbsp;&nbsp;&nbsp;" .
> $errorcount .") <font color=\"red\">Email Address</font>
> field does not contain @<br> \n";
> }
> else if (preg_match_all("/(.)/",
> $_POST['Travel_Request_Email_Address'], $match) < 1)
> {
> $errorcount++;
> $error = $error . "&nbsp;&nbsp;&nbsp;&nbsp;" .
> $errorcount .") <font color=\"red\">Email Address</font>
> field does not contain .<br> \n";
> }
> else
> {
> $_SESSION['Travel_Request_Email_Address'] =
> $_POST['Travel_Request_Email_Address'];
> }
> }
> else
> {
> $errorcount++;
> $error = $error . "&nbsp;&nbsp;&nbsp;&nbsp;" .
> $errorcount .") <font color=\"red\">Email Address</font>
> field is empty<br>\n";
> }
>
> Thanks,
>
> Jake McHenry
> Nittany Travel MIS Coordinator
> http://www.nittanytravel.com
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



I think I got it, I was just messing with the code, and added a slash
before the ., and it seems to work. Is this the correct way to handle
this?



Thanks,

Jake McHenry
Nittany Travel MIS Coordinator
http://www.nittanytravel.com
Reply With Quote
  #3 (permalink)  
Old 11-13-2003
John Nichel
 
Posts: n/a
Default Re: [PHP] preg question

Jake McHenry wrote:

>>-----Original Message-----
>>From: Jake McHenry [mailto:linux@nittanytravel.com]
>>Sent: Thursday, November 13, 2003 2:50 PM
>>To: php-general@lists.php.net
>>Subject: [php] preg question
>>
>>
>>Hi all,
>>
>>I am trying to do a simple validation of an email address
>>being submitted. I have the @ sign being validated, but I
>>can't get the period to work.. Can someone help me out?
>>
>>Here's my code..
>>
>> if ($_POST['Travel_Request_Email_Address'] != "")
>> {
>> if (preg_match_all("/(@)/",
>>$_POST['Travel_Request_Email_Address'], $match) != 1)
>> {
>> $errorcount++;
>> $error = $error . "&nbsp;&nbsp;&nbsp;&nbsp;" .
>>$errorcount .") <font color=\"red\">Email Address</font>
>>field does not contain @<br> \n";
>> }
>> else if (preg_match_all("/(.)/",
>>$_POST['Travel_Request_Email_Address'], $match) < 1)
>> {
>> $errorcount++;
>> $error = $error . "&nbsp;&nbsp;&nbsp;&nbsp;" .
>>$errorcount .") <font color=\"red\">Email Address</font>
>>field does not contain .<br> \n";
>> }
>> else
>> {
>> $_SESSION['Travel_Request_Email_Address'] =
>>$_POST['Travel_Request_Email_Address'];
>> }
>> }
>> else
>> {
>> $errorcount++;
>> $error = $error . "&nbsp;&nbsp;&nbsp;&nbsp;" .
>>$errorcount .") <font color=\"red\">Email Address</font>
>>field is empty<br>\n";
>> }
>>
>>Thanks,
>>
>>Jake McHenry
>>Nittany Travel MIS Coordinator
>>http://www.nittanytravel.com
>>
>>--
>>PHP General Mailing List (http://www.php.net/)
>>To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>

>
>
>
> I think I got it, I was just messing with the code, and added a slash
> before the ., and it seems to work. Is this the correct way to handle
> this?


Yes, you have to escape the period. If you're just checking to see if
there's an at symbol and a period, you don't really need preg_match_all

if ( ! preg_match ( "/@/", $email ) ) {
// No @ symbol
}
if ( ! preg_match ( "/\./", $email ) ) {
// No period
}

--
By-Tor.com
It's all about the Rush
http://www.by-tor.com
Reply With Quote
  #4 (permalink)  
Old 11-13-2003
Kevin Stone
 
Posts: n/a
Default RE: [PHP] preg question

The period is a modifier within the expression (see manual on "pattern
syntax" link below). You need to escape it with a back slash:

\.

http://www.php.net/manual/en/pcre.pattern.syntax.php

--
Kevin


-----Original Message-----
From: Jake McHenry [mailto:linux@nittanytravel.com]
Sent: Thursday, November 13, 2003 12:50 PM
To: php-general@lists.php.net
Subject: [php] preg question

Hi all,

I am trying to do a simple validation of an email address being
submitted. I have the @ sign being validated, but I can't get the
period to work.. Can someone help me out?

Here's my code..

if ($_POST['Travel_Request_Email_Address'] != "")
{
if (preg_match_all("/(@)/",
$_POST['Travel_Request_Email_Address'], $match) != 1)
{
$errorcount++;
$error = $error . "&nbsp;&nbsp;&nbsp;&nbsp;" . $errorcount .")
<font color=\"red\">Email Address</font> field does not contain @<br>
\n";
}
else if (preg_match_all("/(.)/",
$_POST['Travel_Request_Email_Address'], $match) < 1)
{
$errorcount++;
$error = $error . "&nbsp;&nbsp;&nbsp;&nbsp;" . $errorcount .")
<font color=\"red\">Email Address</font> field does not contain .<br>
\n";
}
else
{
$_SESSION['Travel_Request_Email_Address'] =
$_POST['Travel_Request_Email_Address'];
}
}
else
{
$errorcount++;
$error = $error . "&nbsp;&nbsp;&nbsp;&nbsp;" . $errorcount .")
<font color=\"red\">Email Address</font> field is empty<br>\n";
}

Thanks,

Jake McHenry
Nittany Travel MIS Coordinator
http://www.nittanytravel.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Reply With Quote
  #5 (permalink)  
Old 11-13-2003
Jake McHenry
 
Posts: n/a
Default RE: [PHP] preg question

What else would I need to check for? I'm tired.. Running on 2 pots of
coffee.. All I can think of is the @ and at least one . After the @,
then at least 2 characters after the last .

I haven't had much experience with regular expresssions, all of the
stuff I've done has been for my intranet, and I didn't really need to
validate.. Now I'm on a project where it's public........

Thanks for the help

Thanks,

Jake McHenry
Nittany Travel MIS Coordinator
http://www.nittanytravel.com




> -----Original Message-----
> From: John Nichel [mailto:jnichel@by-tor.com]
> Sent: Thursday, November 13, 2003 3:09 PM
> To: Jake McHenry
> Cc: php-general@lists.php.net
> Subject: Re: [php] preg question
>
>
> Jake McHenry wrote:
>
> >>-----Original Message-----
> >>From: Jake McHenry [mailto:linux@nittanytravel.com]
> >>Sent: Thursday, November 13, 2003 2:50 PM
> >>To: php-general@lists.php.net
> >>Subject: [php] preg question
> >>
> >>
> >>Hi all,
> >>
> >>I am trying to do a simple validation of an email address
> >>being submitted. I have the @ sign being validated, but I
> >>can't get the period to work.. Can someone help me out?
> >>
> >>Here's my code..
> >>
> >> if ($_POST['Travel_Request_Email_Address'] != "")
> >> {
> >> if (preg_match_all("/(@)/",
> >>$_POST['Travel_Request_Email_Address'], $match) != 1)
> >> {
> >> $errorcount++;
> >> $error = $error . "&nbsp;&nbsp;&nbsp;&nbsp;" .
> >>$errorcount .") <font color=\"red\">Email Address</font>
> >>field does not contain @<br> \n";
> >> }
> >> else if (preg_match_all("/(.)/",
> >>$_POST['Travel_Request_Email_Address'], $match) < 1)
> >> {
> >> $errorcount++;
> >> $error = $error . "&nbsp;&nbsp;&nbsp;&nbsp;" .
> >>$errorcount .") <font color=\"red\">Email Address</font>
> >>field does not contain .<br> \n";
> >> }
> >> else
> >> {
> >> $_SESSION['Travel_Request_Email_Address'] =
> >>$_POST['Travel_Request_Email_Address'];
> >> }
> >> }
> >> else
> >> {
> >> $errorcount++;
> >> $error = $error . "&nbsp;&nbsp;&nbsp;&nbsp;" .
> >>$errorcount .") <font color=\"red\">Email Address</font>
> >>field is empty<br>\n";
> >> }
> >>
> >>Thanks,
> >>
> >>Jake McHenry
> >>Nittany Travel MIS Coordinator
> >>http://www.nittanytravel.com
> >>
> >>--
> >>PHP General Mailing List (http://www.php.net/)
> >>To unsubscribe, visit: http://www.php.net/unsub.php
> >>
> >>

> >
> >
> >
> > I think I got it, I was just messing with the code, and

> added a slash
> > before the ., and it seems to work. Is this the correct way

> to handle
> > this?

>
> Yes, you have to escape the period. If you're just checking
> to see if
> there's an at symbol and a period, you don't really need
> preg_match_all
>
> if ( ! preg_match ( "/@/", $email ) ) {
> // No @ symbol
> }
> if ( ! preg_match ( "/\./", $email ) ) {
> // No period
> }
>
> --
> By-Tor.com
> It's all about the Rush
> http://www.by-tor.com
>

Reply With Quote
  #6 (permalink)  
Old 11-13-2003
Zhuravlev Alexander
 
Posts: n/a
Default Re: [PHP] preg question

On Thu, Nov 13, 2003 at 03:17:14PM -0500, Jake McHenry wrote:
> What else would I need to check for? I'm tired.. Running on 2 pots of
> coffee.. All I can think of is the @ and at least one . After the @,
> then at least 2 characters after the last .
>
> I haven't had much experience with regular expresssions, all of the
> stuff I've done has been for my intranet, and I didn't really need to
> validate.. Now I'm on a project where it's public........


http://www.zend.com/codex.php?id=861&single=1
http://www.zend.com/codex.php?id=88&single=1
Reply With Quote
  #7 (permalink)  
Old 11-13-2003
Jake McHenry
 
Posts: n/a
Default RE: [PHP] preg question

> -----Original Message-----
> From: zhuravlev alexander [mailto:a_zhuravlev@hotmail.com]
> Sent: Thursday, November 13, 2003 3:26 PM
> To: Jake McHenry
> Cc: 'John Nichel'; php-general@lists.php.net
> Subject: Re: [php] preg question
>
>
> On Thu, Nov 13, 2003 at 03:17:14PM -0500, Jake McHenry wrote:
> > What else would I need to check for? I'm tired.. Running on

> 2 pots of
> > coffee.. All I can think of is the @ and at least one .

> After the @,
> > then at least 2 characters after the last .
> >
> > I haven't had much experience with regular expresssions, all of

the
> > stuff I've done has been for my intranet, and I didn't

> really need to
> > validate.. Now I'm on a project where it's public........

>

http://www.zend.com/codex.php?id=861&single=1
http://www.zend.com/codex.php?id=88&single=1



Wow, thanks for the links... That definatly speeds things up a little
for me.



Thanks,

Jake McHenry
Nittany Travel MIS Coordinator
http://www.nittanytravel.com
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 04:57 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0