Re: Please can any one sugest or send me Email validation code.
Re: Please can any one sugest or send me Email validation code.
- Subject: Re: Please can any one sugest or send me Email validation code.
- From: "Jim Thomason" <email@hidden>
- Date: Thu, 1 Feb 2007 08:47:08 -0600
There's a regex for it in Jeffrey Friedl's "Mastering Regular
Expressions", from O'Reilly. I think it's like 4,700 characters, IIRC.
BUT - it's actually not -that- bad. For example, being a
do-it-yourselfer, I crafted a regex to match valid email addresses
myself for my perl module Mail::Bulkmail. It's "only" about 900
characters, I think.
The problem is that email addresses can contain comments, and that the
blasted things can recurse infinitely.
email@hidden
foo(this is a comment)@bar.com
foo(this is a (this is a nested comment!) comment)@bar.c(other comment)om
are all the same email address. The regex in the book allowed two
levels of nesting, so it was much longer. Mine allowed no comments (I
stripped them out in an earlier step), so it was much shorter. YMMV.
If you want to roll your own, read RFC 822 and RFC 2822. But, I really
wouldn't recommend it unless you're really gungho about seeing if you
can do it on your own. If you just want to get the thing going, there
are better approaches.
First of all, if you can't find a cocoa implementation, look for a C
one. I'm sure there's a C email parser somewhere around and wrappering
it into an objective-c class would be trivial.
Second, if you can't find that, then try one of the myriad modules
written in other languages. For example, Perl has one called
Email::Valid which is pretty good. You can embed the perl script + the
library in your code and create a task to execute the script and look
at the results coming back (true vs. false).
Finally, realistically? I've found over the years that it's really
rarely worth the trouble. Sure, the RFCs state that I can type in:
"Foo Bar"<email@hidden>
for my address or:
f(comment)o(comment)o(comment)@(comment)b(comment)a(comment)r(comment).(comment)c(comment)o(comment)m(comment)
and they're both 100% valid email addresses that should get to me.
But, realistically? I'll type in email@hidden and nothing else. 99+% of
the net agrees.
I tend to just use an extremely simple regex:
.+@.+\..+
Or, to translate, "something at something dot something". it lets by
addresses that look valid-ish and then I let the MTA worry about
weeding out something nasty at a later date. Of course, this assumes
that your users are actually going to want to type in a valid email
address and you're just trying to weed out typos. If they don't
actually want to give you an address, they'll just put in a fake one
(or a spam bucket one) and ignore you completely. Otherwise, they'll
either give you one from the start, or (in order) will first leave the
field blank, then type in foo, then type in email@hidden. No need for
fancy validation at all.
-Jim....
On 2/1/07, Marc Wan <email@hidden> wrote:
this is good advice, and i will add the following:
- all of the rules for valid email addresses are extremely complicated. i
believe there was one attempt made at a regular expression for this,
and it is rumoured to be nightmarish.
- however, do NOT take the easy route and simply assume an email address is:
[a-zA-Z0-9-\.]@{basically the same}
I can't tell how many websites refuse to let me use
"email@hidden". Gmail, in particular, makes frequent and
excellent use of the "+" character.
m.
On 2/1/07, Marc Wan <email@hidden> wrote:
> this is good advice, and i will add the following:
>
> - actualy valid email addresses are extremely complicated. i
> believe there was one attempt made at a regular expression for this,
> and it is rumoured to be nightmarish.
>
> - however, do NOT take the easy route and simply assume an email address is:
>
> [a-zA-Z0-9-\.]@{basically the same}
>
> I can't tell how many websites refuse to let me use
> "email@hidden". Gmail, in particular, makes frequent and
> excellent use of the "+" character.
>
> m.
>
>
>
>
> On 2/1/07, Keary Suska <email@hidden> wrote:
> > on 1/30/07 10:52 PM, email@hidden purportedly said:
> >
> > > I am new to Cocoa i am devloping one application in that i need to do
> > > validation for email.
> > > please can any one sugest me on this or send me the code.
> >
> > As mentioned, it depends on what "validation" means. If you mean to check
> > whether an entered value is a syntactically valid email, as long as you know
> > the correct syntax (read the relevant RFCs), you then need to determine
> > "when" validation occurs and by what method. Common approaches are via
> > NSFormatter subclasses and Key-Value-Coding validation. Next to determine
> > the actual code for checking validity. You can investigate the NSString and
> > NSScanner classes for methods to parse strings.
> >
> > I have found AppKiDo (http://homepage.mac.com/aglee/downloads) to be an
> > invaluable resource for quickly browsing classes. I highly recommend it.
> >
> > Best,
> >
> > Keary Suska
> > Esoteritech, Inc.
> > "Demystifying technology for your home or business"
> >
> >
> > _______________________________________________
> >
> > Cocoa-dev mailing list (email@hidden)
> >
> > Do not post admin requests or moderator comments to the list.
> > Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> >
> > Help/Unsubscribe/Update your Subscription:
> >
> > This email sent to email@hidden
> >
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden