note 78795 added to function.preg-match

From: Date: Sun, 28 Oct 2007 09:12:31 +0000
Subject: note 78795 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-131786@lists.php.net to get a copy of this message
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. ---- Server IP: 69.147.83.197 Probable Submitter: 83.177.70.247 ---- Manual Page -- http://www.php.net/manual/en/function.preg-match.php Edit -- https://master.php.net/note/edit/78795 Del: integrated -- https://master.php.net/note/delete/78795/integrated Del: useless -- https://master.php.net/note/delete/78795/useless Del: bad code -- https://master.php.net/note/delete/78795/bad+code Del: spam -- https://master.php.net/note/delete/78795/spam Del: non-english -- https://master.php.net/note/delete/78795/non-english Del: in docs -- https://master.php.net/note/delete/78795/in+docs Del: other reasons-- https://master.php.net/note/delete/78795 Reject -- https://master.php.net/note/reject/78795 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#131786) next »