note 29111 modified in function.ereg by victor

From: Date: Mon, 03 Feb 2003 07:13:54 +0000
Subject: note 29111 modified in function.ereg by victor
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-43315@lists.php.net to get a copy of this message
[Editor's Note: This perhaps works in most cases, but for example foo@bar.info, which is a valid address, doesn't pass. Allowing just a fixed set of top level domains will get you into trouble when they add new ones. Info, biz, aero etc are all TLDs introduced in 2001] I've been working on this email validation posix for about 4 hours now...here is what I've come up with: <?php $email = 'example@email.mafia.UK'; function validate_email($str){ $str = strtolower($str); if(ereg("^([^[:space:]][a-zA-Z0-9#_\-]+)@ ([^[:space:]][a-zA-Z0-9._\-]+)\.(ad|ae|af|ag|ai|al|am|an|ao| aq|ar|arpa|as| at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm| bn|bo|br|bs|bt|bv|bw|by|bz|ca |cc|cd|cf|cg|ch|ci|ck|cl| cm|cn|co|com|cr|cu|cv|cx|cy|cz|de|dj|dk| dm|do|dz|ec|edu|ee|eg|eh|er|es|et|fi|fj|fk|fm|fo|fr|fx|ga| gb|gov|gd|ge|gf| gh|gi|gl|gm|gn|gp|gq|gr|gs| gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|int |io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp| kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt| lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml| mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my| mz|na|nato|nc|ne|net|nf|ng|ni|nl|no|np|nr |nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn |pr|pt|pw|py |qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj |sk|sl|sm|sn|so|sr|st|sv|sy|sz|tc|td|tf|tg| th|tj|tk|tm|tn|to|tp|tr| tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve| vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$",$str)){ return 1; } else { return 0; } } if(validate_email($email)){ echo "$email is good."; } else { echo "$email is bad."; } ?> NOTE: you'll have to remove the newlines so that the posix it all on one line, i had to add the newlines so that the note wouldn't be too long to be added. It should check for a pretty close format, and check all of the top level domains. --was-- I've been working on this email validation posix for about 4 hours now...here is what I've come up with: <?php $email = 'example@email.mafia.UK'; function validate_email($str){ $str = strtolower($str); if(ereg("^([^[:space:]][a-zA-Z0-9#_\-]+)@ ([^[:space:]][a-zA-Z0-9._\-]+)\.(ad|ae|af|ag|ai|al|am|an|ao| aq|ar|arpa|as| at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm| bn|bo|br|bs|bt|bv|bw|by|bz|ca |cc|cd|cf|cg|ch|ci|ck|cl| cm|cn|co|com|cr|cu|cv|cx|cy|cz|de|dj|dk| dm|do|dz|ec|edu|ee|eg|eh|er|es|et|fi|fj|fk|fm|fo|fr|fx|ga| gb|gov|gd|ge|gf| gh|gi|gl|gm|gn|gp|gq|gr|gs| gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|int |io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp| kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt| lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml| mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my| mz|na|nato|nc|ne|net|nf|ng|ni|nl|no|np|nr |nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn |pr|pt|pw|py |qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj |sk|sl|sm|sn|so|sr|st|sv|sy|sz|tc|td|tf|tg| th|tj|tk|tm|tn|to|tp|tr| tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve| vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$",$str)){ return 1; } else { return 0; } } if(validate_email($email)){ echo "$email is good."; } else { echo "$email is bad."; } ?> NOTE: you'll have to remove the newlines so that the posix it all on one line, i had to add the newlines so that the note wouldn't be too long to be added. It should check for a pretty close format, and check all of the top level domains. http://www.php.net/manual/en/function.ereg.php

« previous php.notes (#43315) next »