473,320 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to check if a character appears in a form variable?

Hi guys!

Can anyone tell me how you can perform validation on a string variable
in PHP so that you can check if a specific character is contained
within the string. The reason for this is i am trying to create a form
for a site i am making and i want to validate the user's input for the
email address field, by ensuring it contains the '@' character.

Cheers.

Sep 23 '05 #1
7 2649
monomaniac21 (ma********@msn.com) wrote:
: Hi guys!

: Can anyone tell me how you can perform validation on a string variable
: in PHP so that you can check if a specific character is contained
: within the string. The reason for this is i am trying to create a form
: for a site i am making and i want to validate the user's input for the
: email address field, by ensuring it contains the '@' character.

That is hardly a check to validate an email address!

However, one function that would help is strstr();

$at = strstr($claimed_email_address,"@");
if ($at == "")
{
echo "no @ found";
die ;
}

But there are dozens of other functions that would also help. The php
manual is available on line (google for it) look for string functions.
--

This programmer available for rent.
Sep 23 '05 #2
Instead, use preg_match to use Perl-style regular expressions.
It can check to see if it found a pattern.
For emails, use something like:

if( preg_match("/\w+@\w+\.\w+",$variable) )
{
do_whatever...;
}

which will check for the format ab*@abc.abc
Sep 23 '05 #3
Mazin07 wrote:
/\w+@\w+\.\w+


Ad hoc regular expressions to validate the syntax of e-mail addresses
are invariably both too loose and too strict. Too loose in that they
pass forms not allowed by RFC2822, too strict in that they reject
forms allowed by RFC2822. Even the Mail::RFC822::Address pattern
doesn't encompass the gamut of forms, but then it doesn't pretend to.

--
Jock
Sep 24 '05 #4
John Dunlop <us*********@john.dunlop.name> wrote:

Mazin07 wrote:
/\w+@\w+\.\w+


Ad hoc regular expressions to validate the syntax of e-mail addresses
are invariably both too loose and too strict. Too loose in that they
pass forms not allowed by RFC2822, too strict in that they reject
forms allowed by RFC2822. Even the Mail::RFC822::Address pattern
doesn't encompass the gamut of forms, but then it doesn't pretend to.


Jeffrey Friedl's book, "Mastering Regular Expressions" contains a complete
regular expression which matches all valid RFC 822 e-mail addresses. It is
more than 6,500 characters long.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Sep 25 '05 #5
Tim Roberts wrote:
Jeffrey Friedl's book, "Mastering Regular Expressions" contains a complete
regular expression which matches all valid RFC 822 e-mail addresses. It is
more than 6,500 characters long.


Interesting. I don't have his book, but is that the pattern
Email::Valid uses?

http://search.cpan.org/src/MAURICE/E...-0.15/Valid.pm

--
Jock
Sep 25 '05 #6
John Dunlop <us*********@john.dunlop.name> wrote:
Tim Roberts wrote:
Jeffrey Friedl's book, "Mastering Regular Expressions" contains a complete
regular expression which matches all valid RFC 822 e-mail addresses. It is
more than 6,500 characters long.


Interesting. I don't have his book, but is that the pattern
Email::Valid uses?

http://search.cpan.org/src/MAURICE/E...-0.15/Valid.pm


That's the one. He credits Friedl in the code.

The Friedl book is actually quite good. Some people think the subject
could be adequately covered in a 3-page man page, but Friedl covers the
tricks, ins, and outs of many of the popular regular expression engines. I
recommend it.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Sep 27 '05 #7
Tim Roberts wrote:
The Friedl book is actually quite good.
[...]
I recommend it.


It's on my wish list, Tim, right below 25-hour days.

--
Jock
Sep 30 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Stefan Richter | last post by:
Hi, I am looking for a good function to check if a entered value is a money value - meaning it's a decimal with no more then 2 digits after the point. Thanks, Stefan
27
by: John Roth | last post by:
PEP 263 is marked finished in the PEP index, however I haven't seen the specified Phase 2 in the list of changes for 2.4 which is when I expected it. Did phase 2 get cancelled, or is it just not...
6
by: Joshua Weston | last post by:
I am new to C++ and I am very interested in becoming proficient. This post is two-fold. One, I am having problems with this small test program. It is meant to allow user input to control a "@"...
2
by: Mike | last post by:
I´ve got a number of SPAN elements named "mySpan1", "mySpan2", "mySpan3" etc, and want to set their "style.display" to "inline". This works (only needs to work on IE5.5+): for (var x = 1; x <...
3
by: Oliver Saunders | last post by:
is there anyway to check if a string could be used a variable name. So return false if the first character isn't a letter or if it contains spaces etc. I started writing a function that would do...
44
by: Kulgan | last post by:
Hi I am struggling to find definitive information on how IE 5.5, 6 and 7 handle character input (I am happy with the display of text). I have two main questions: 1. Does IE automaticall...
24
by: garyusenet | last post by:
I'm working on a data file and can't find any common delimmiters in the file to indicate the end of one row of data and the start of the next. Rows are not on individual lines but run accross...
5
by: Just D | last post by:
All, Any valuable idea about subj: "The '%' character, hexadecimal value 0x25" ? I tried to google, but nothing interesting was found. Is it IIS settings problem, user side problem or...
4
by: jameskuyper | last post by:
mkeles84 wrote: The other responses you've received have explained why 'name' needs to contain a Null-Terminated Character String (NTCS). However, it occurred to me that you might not be aware...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.