note 78795 deleted from function.preg-match by felipe

From: Date: Sun, 23 Mar 2008 22:55:46 +0000
Subject: note 78795 deleted from function.preg-match by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-137375@lists.php.net to get a copy of this message
Note Submitter: frebby at gmail dot com ---- If you wonder how to check for correct e-mail and such (you can use it for usernames and anything you want, but this is for e-mail) you can use this little code to validate the users e-mail: We'll assume that they have been processing a form, entering their e-mail as "email" and now PHP will take care of the rest: $emailcheck = $_POST["email"]; if(!preg_match("/^[a-z0-9\å\ä\ö._-]+@ [a-z0-9\å\ä\ö.-]+\.[a-z]{2,6}$/i", $emailcheck)) $errors[] = "- Your e-mail is missing or is not valid."; (note that the preg_match had to be cut or I couldn't post it since it was too long so I cut it after @ so just put them together again.) If we split the parts it would look like this: [a-z0-9._-]+@ This is the name of the email, such as greatguy3 (then @domain.com) so this allows dot, underscore and - aswell as alphabetical letters and decimals. [a-z0-9.-]+\. This is the domain part, note that there must be a dot after domain name, so it's harder to fake an email. Same here though, A-Z, 0-9, dot and - (if your domain has - in it, such as nintendo-wii.com) [a-z]{2,6}$/i This is the last part of your email, the .com/.net/.info, whichever you use. The numbers between {} is how many letters are limited (in this case min 2 and max 6) and it would allow "us" up to "org.uk" and "museum" and only A-Z letters are used for obvious reasons. The "i" there is there so you can use both uppercase and lowercase characters. (A-Z & a-z) So a valid email address with this code would be "coolguy3@cooldomain.com" and a nonvalid one would be "zûmgz^;*@hot_mail.bananá" This is only the email part, so this is not the fullcode. Paste this in your form process to use it with the rest of your code! Hope this helps.

« previous php.notes (#137375) next »