Anyone have any good code or links for validating an email address?
Thanks
Printable View
Anyone have any good code or links for validating an email address?
Thanks
Thanks but I know google. Im looking more for known solid code.
Look at my sig. I have an AJAX tutorial - one of the examples is email verification. The code can also be applied in a no dynamic environment too.
In the code. I.e: to validate the best you can do is:
- A regular expression.
- A look up on the domain name to verify it exists.
The best way and only way to be sure is to verify. Validation and verification are both desirable in a two staged approach.
And what exactly do you mean by "solid" code? Google is the best place to find some of the regular expressions and libraries out there which can and are being used. Why re-invent the wheel?Quote:
Originally Posted by RobDog888
Thanks, the articles examples should help but I just need the email validation of a correct format entry. No return email sending needed.
Regex is fine and about all I need but what is the syntax needed?
I already have captcha verification to prevent spammers too.
The proper regular expression to validate an email address is approximately the length of War and Peace and is thus completely inefficient.
I take the view that as long as it contains one '@' symbol it is highly statistically likely to be valid and at that point one should verify it by sending a confirmation message containing an activation code.
I usually check for an @ symbol and at least one a-z0-9_ and at least one dot in the domain.
Gimme c0d for it containing an "@" and a "." and minimum length of 8 :D
Gimme, gimme, gimme c0d, gimme c0d ... :lol:
You don't deserve this but:
/.+@[a-z0-9_](.[a-z0-9_])+/i
To be used with preg_match.
a@b is a valid address...Quote:
Originally Posted by visualAd
Only internally.
And?
Requiring a FDN is perfectly acceptable if your application is designed for external users. Because what the validation is doing is detecting human error, nothing more; nothing less. It is quite likely that if the domain name is not fully qualified and the site is designed for external users that the user has made a mistake.
Why not :(Quote:
Originally Posted by visualAd
Shouldnt the minimum valid format be "[email protected]"? Minimum 7 characters?
[email protected] will match that regular expression.
What's the problem?
Better.Quote:
Originally Posted by visualAd
However I would still rather verify than attempt to validate the address. If you can send a verification email then validation is of course unnecessary.
[email protected], if you require a TLD. Not all are three characters; China, for example, is simply .cn.Quote:
Originally Posted by RobDog888
you said I dont desreve it :D
So thats the regex but how about this prego_match function? Is it a php built in function?
I believe that is a Spears family function.Quote:
Originally Posted by RobDog888
However, you may be looking for preg_match.
What would be "[email protected]" then? ".a" stands for a real country extension or is the minimum country length is 2?
What the heck is all the complication with the Spears function? PREG_OFFSET_CAPTURE? I just want to regex the string to see if its a valid string or not. I just cant get into this php code.
Like this? :(
PHP Code:$pattern = '/.+@[a-z0-9_](.[a-z0-9_])+/i';
preg_match($pattern, txtEmail, $matches, PREG_OFFSET_CAPTURE);
It's not real (at the moment) but I don't believe there is a minimum length.Quote:
Originally Posted by RobDog888
You could try RFC1034, if you can be bothered.
Also, for entertainment purposes, here is a regular expression to validate addresses against RFC822:
http://ex-parrot.com/pdw/Mail-RFC822-Address.html
Yes, except most of the parameters are unnecessary.Quote:
Originally Posted by RobDog888
PHP Code:if (preg_match('/.+@[a-z0-9_](.[a-z0-9_])+/i', $address) === 1)
{
# Valid.
}
else
{
# Invalid.
}
Isnt there some utility or site that generates regex patterns?
What does the offset constant signify other then some kind of lame offset?
What should it be then to shorten it to just the necessary chars?
Thanks for the help so far guys
The validation and verification are necessary if you want to ensure the email address is correct.
I use a utility called the brain to generate regular expressions - I won't tell you whose brain though :D
Although PHP does have a good guide to PCRE - http://us3.php.net/manual/en/referen...ern.syntax.php
In addition you need to use the ^ and $ assertions to anchor the email address to the beginning and end of the string respectively otherwise you could have something like this go though:
Code:i am a naughty mistress & love to sell 100TB HD'[email protected] house.
'/^.+@[a-z0-9_](.[a-z0-9_])+$/i'
If I have them enter in their email twice in separate textboxes, do comparisions that they match and verify the correct email format then that should be enough (at least for now).
Ps, where can I download "brain" for generating regex?
They are really not as scary as they look. My point is the best person to generate a regular expression is you - as it is like a template for a string you need to create a regular expression to test for compliance to that format.
The best option is to Google and find out if anyone has written one already - if not you are on your own.
I have this code in the regex sucessful match block but I am getting a parse error :mad:
PHP Code:mail(
webmaster@example.com,
"Something for the subject",
"Email - ".$_POST['txtEmail1']."\r\n".
$_POST['txtMessage']."\r\n\r\n".
"Contact message from website.",
"From: noreply@{$_SERVER['HTTP_HOST']}\r\n"
);
It's commonplace to put quotes around strings. :D
Um I have it that way or am I missing something? :(
Or are you meaning around the $_POST part like I just added below?
Edit: ARRRG! Still errors. I hate php :mad: :lol:Code:"$_POST['txtMessage']"
solved it. I needed the curly braces around the post functions.
Code:"{$_POST['txtMessage']}\r\n\r\n"
Thanks everyone! :)