|
|
|
|
![]() ![]() |
Nov 27 2004, 11:05 AM
Post
#1
|
|
|
BUG.SWAT.PATROL Group: Members Posts: 626 Joined: 1-September 04 From: Auckland, New Zealand Member No.: 27 |
I am just wondering what you would do to validate against an email address, basically I want to know what Regular Expression you would use to check for a valid format. I've seen so many different ways of doing it, some I can definitely say can produce wrong results, but I'm not saying mine is perfect either, basically I want to perfect it.
Here's what I have, I use PHP's function eregi for this, so case is not important. CODE ^[a-z][_.a-z0-9-]+@[a-z0-9][a-z0-9-]+\.[a-z]+([.]?[a-z][a-z]+)* What I wanted to achieve was the first character of any email address should be a letter if I'm not mistaken, I probably should go back and read the RFC 2822 as this should have been my first stop in understanding how emails are handled and what is accepted and what isn't. Next I allow an alphanumeric words with underscore, dot and hyphen (minus), next comes the @ sign, after it I check again if the first character is a letter or number, after that another alphanumeric word which allows hypens as well, I don't think you can have an underscore in a domain name?, then comes a dot, then only a word with only letters, I'm still wondering whether to include numbers or not, but so far seen no signs of why I should. The last bit is the trickiest to make it work, basically it checks if another dot follows, if so it checks that there's a letter afterwards then a word of only letters, so there must exist at least 2 letters after the dot, if not, no match, it then continuously checks it over an over again with the additional dots. Which means something like a2z-abc_def@a2z-abc.com.net.co.uk is valid, even if it does not exist, but that's where checking the host comes into play. It's that end part that worries me a bit though as it can accept a large amount of dots and words over and over again, although I maybe able to find a limit in the RFC's so I will fix that accordingly to it, else it may be a problem. Other than that, after verifying if the email is valid, I would then check against DNS records or Hostname to see if the domain of the email exists, I could query the sever if the user exists but most servers suggest the user does even when they don't so I rather see if the host exists instead. So what can you suggest to improve it? I know about shortform ways of writing it, but this is how eregi accepts it. I will now go off and read the RFCs on it, might look at the earlier one first before the newest. Cheers, MC |
|
|
|
Nov 27 2004, 11:40 AM
Post
#2
|
|
|
Premium Member Group: [HOSTED] Posts: 336 Joined: 22-September 04 Member No.: 798 |
you can send them a link to validate their address...
you put the emailaddress in your db in a table that has two fields: the address and a boolean value, default 0 and send them a link like: yourhost.com/validate?mail=theiremail and they you just put the boolean on 1 for the reg expression, can't really think of anything better than the one you have oh yeah, by the way, tnx for the linux boot thingy: i inserted my 1st cd, entered repair on boot and i was given the option to repair my bootloader! hurray! tnx mate |
|
|
|
Nov 27 2004, 12:19 PM
Post
#3
|
|
|
BUG.SWAT.PATROL Group: Members Posts: 626 Joined: 1-September 04 From: Auckland, New Zealand Member No.: 27 |
QUOTE(marijnnn @ Nov 28 2004, 12:40 AM) you can send them a link to validate their address... you put the emailaddress in your db in a table that has two fields: the address and a boolean value, default 0 and send them a link like: yourhost.com/validate?mail=theiremail and they you just put the boolean on 1 for the reg expression, can't really think of anything better than the one you have oh yeah, by the way, tnx for the linux boot thingy: i inserted my 1st cd, entered repair on boot and i was given the option to repair my bootloader! hurray! tnx mate I understand the confirmation email side of things, although this is no membership system but rather just an ordinary form fill out where I want to eliminate any errors. I mean I could do multiple checks on it, splitting it etc, but I wanted a sure fire, basically one liner way of checking it, although now that I've thought about multiple checks, maybe I can look at how many checks I could possibly do to make sure it's valid and then try to build from that and simplify it to just a one liner. I'm glad you got your linux sorted. Cheers, MC |
|
|
|
Dec 14 2004, 03:12 AM
Post
#4
|
|
|
Advanced Member Group: Members Posts: 145 Joined: 13-December 04 Member No.: 1,734 |
mastercomputers:
so you want to validate email addresses? i would like to advice that you should use Client Side Scripting for that manner. This includes JavaScript and VBScript. This way, the validation will only be done at client, thus eliminating any unnecessary burden to the server and minimize the use for your bandwidth. where to find the example? click your mouse to: http://www.hotscript.com/ do a search, enter the keyword "email validation" and select "JavaScript" in the category. easy and simple. |
|
|
|
Dec 19 2004, 09:13 PM
Post
#5
|
|
|
Member - Active Contributor Group: Members Posts: 77 Joined: 11-December 04 Member No.: 1,704 |
Try PCRE (Perl Compatible Regular Expressions) (preg_*), it's much faster.
And, convert the email address to all lowercase first. CODE <^[a-z]{1}[a-z0-9_\.\-]+@[a-z0-9]{1}[a-z0-9\-]+\.[a-z0-9\-\.]+$>
|
|
|
|
Dec 24 2004, 01:22 PM
Post
#6
|
|
|
BUG.SWAT.PATROL Group: Members Posts: 626 Joined: 1-September 04 From: Auckland, New Zealand Member No.: 27 |
QUOTE(cryptwizard @ Dec 20 2004, 10:13 AM) Try PCRE (Perl Compatible Regular Expressions) (preg_*), it's much faster. And, convert the email address to all lowercase first. CODE <^[a-z]{1}[a-z0-9_\.\-]+@[a-z0-9]{1}[a-z0-9\-]+\.[a-z0-9\-\.]+$> I changed mine a bit and now and also I use preg_match, it now looks like CODE if(preg_match("/^[a-z][a-z0-9_.-]+@[a-z0-9][a-z0-9-]+\.[a-z]+(\.[a-z]+)*$/i", $email)) I think I will settle on this as the method although I will work out the minimum values, as that's my last resort for this, far from perfect but it does meet most problems I have seen. I will also try and see if I can find any performance difference in converting to lowercase and removing the case insensitive bit, escaping the characters or enclosing them in braces as it's just a small thing for now. If you look at cryptwizards code, the only problem I can see is on the end \.[a-z0-9\-\.] which will say blah_this@abc-def.com.au to be valid, but it also allows blah_this@abc-def.....com....au.. to be valid too. After every dot, it's probably a good idea to check for a letter or number, but not if it's in the first part before the @ sign. The last bit of an email, usually falls in the line of net, biz, com, org, etc but you can also get com.au, co.uk, etc. I believe there's no dashes in this part so I assume it's safe to leave those out. If there ever was a problem brought to my attention about this I would fix it promptly, but for now, I find my code fitting for it's purpose. I have thought about client side checking, but I'm still left using the server to verify the rest of the data, so decided to do the whole thing server side, it's all built into my validator class so it's probably best to try and keep the code altogether than to partially split them between client and server. I also have less trust in client checking than server checking, considering that if I did do client side checking, I'd still use the server to verify it again, which means double the effort. Thanks for the pointers though. Cheers, MC |
|
|
|
Jan 12 2005, 06:47 AM
Post
#7
|
|
|
Member [ Level 1 ] Group: Members Posts: 34 Joined: 12-December 04 Member No.: 1,718 |
If you were using Perl you could use:
CODE if( $email =~ m{\b(([\w._-]+)\@([\w._-]+))}) { //code follows here } For more about Regular Expressions, visit www.regularexpressions.info. Great Site |
|
|
|
May 31 2005, 09:21 PM
Post
#8
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 12 Joined: 31-May 05 Member No.: 5,632 |
Short answer, it's impossible. even if you used RFC's issued 2000+ character regular expression, it would still fail some perfectly valid, deliverable addresses.
As far as checking the host, it can still be thrown off by gobbleDgook@hotmail.com. In response to the suggestion of using client-side scripts, lots of people (myself included) have them disabled most of the time. so, the onl way you can truly alidate an e-mail address is to actually send an e-mail to it |
|
|
|
Mar 21 2006, 12:09 AM
Post
#9
|
|
|
Member [ Level 2 ] Group: Members Posts: 53 Joined: 20-March 06 Member No.: 12,139 |
Notice from szupie:
Perl Email Syntax Checker QUOTE Use this code to test an email address for proper syntax. It will return '1' if the email address passes or a '0' if it fails. You can use it in conjunction with the Ace Perl Popup Window to let the user know they entered an invalid email address. For example: if(!&check_email(email)){ &PError("Error. Invalid email address."); } Usage (Step 1): Use this code to check the email address. $valid will be '1' or true if it passes or a '0' or false if it fails. Replace [EMAIL] with the variable name you want to check CODE $valid=&check_email("[EMAIL]"); Code (Step 2): Add the following code somewhere within your script CODE sub check_email { # Initialize local email variable with input to subroutine. # $email = $_[0]; # If the e-mail address contains: # if ($email =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/ || # the e-mail address contains an invalid syntax. Or, if the # # syntax does not match the following regular expression pattern # # it fails basic syntax verification. # $email !~ /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/) { # Basic syntax requires: one or more characters before the @ sign, # # followed by an optional '[', then any number of letters, numbers, # # dashes or periods (valid domain/IP characters) ending in a period # # and then 2 or 3 letters (for domain suffixes) or 1 to 3 numbers # # (for IP addresses). An ending bracket is also allowed as it is # # valid syntax to have an email address like: user@[255.255.255.0] # # Return a false value, since the e-mail address did not pass valid # # syntax. # return 0; } else { # Return a true value, e-mail verification passed. # return 1; } } cheers! shiv |
|
|
|
Mar 21 2006, 12:31 AM
Post
#10
|
|
|
Super Member Group: Members Posts: 572 Joined: 25-April 05 From: Nashville Tennessee Member No.: 4,340 |
You could always use PHP
CODE <?php $email="john@zend.com"; if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) { echo "The e-mail was not valid"; } else { echo "The e-mail was valid"; } ?> Yes I know that this is a Perl & CGI but PHP is considered cgi and server sides like javasxript with a user not haing it enabled will not work while the above will. Plus it takes care of .de.tk.anything with two character tlds. This post has been edited by Houdini: Mar 21 2006, 12:34 AM |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 7th October 2008 - 08:09 AM |