note 90437 modified in function.ereg by danbrown

From: Date: Wed, 22 Apr 2009 12:54:16 +0000
Subject: note 90437 modified in function.ereg by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-153583@lists.php.net to get a copy of this message
Here a little clear and easy script that I wrote to validate an E-Mail Input. I tried to understand ereg, but the describtion on php.net did not help me, so I made some research and finally (think) that Im sure how to use ereg. The following function works for me well and allows an E-Mail to contain the following characters: "A-Z, a-z, 0-9, . , - , _ " <?php function validateMail($mail) { if($mail !== "") { if(!ereg("^[A-Za-z0-9\.|-|_]* [@]{1}[A-Za-z0-9\.|-|_]*[.]{1}[a-z]{2,5}$", $mail)) { echo "wrong Input for E-Mail"; $mail =""; return $mail; } else { return $mail; } } } ?> to make it clear: ^ means begin here (startpoint of ereg) $ means end here (endpoint of ereg) * stand for endless characters (I want A-Z, a-z, 0-9 and the other characters to apear from 0 to endless optional you can write {1,5} for min 1 times a character or max 5 times a characters instead of * {1,2} means min 1 time a character and max 2 times a character (optional you can write * intead of {1,2} {1} stand for max 1 time a character \ means from here special characters (like .-_$ and so on) | means OR --was-- Here a little clear and easy script that I wrote to validate an E-Mail Input. I tried to understand ereg, but the describtion on php.net did not help me, so I made some research and finally (think) that Im sure how to use ereg. The following function works for me well and allows an E-Mail to contain the following characters: "A-Z, a-z, 0-9, . , - , _ " function validateMail($mail) { if($mail !== "") { if(!ereg("^[A-Za-z0-9\.|-|_]* [@]{1}[A-Za-z0-9\.|-|_]*[.]{1}[a-z]{2,5}$", $mail)) { echo "wrong Input for E-Mail"; $mail =""; return $mail; } else { return $mail; } } } to make it clear: ^ means begin here (startpoint of ereg) $ means end here (endpoint of ereg) * stand for endless characters (I want A-Z, a-z, 0-9 and the other characters to apear from 0 to endless optional you can write {1,5} for min 1 times a character or max 5 times a characters instead of * {1,2} means min 1 time a character and max 2 times a character (optional you can write * intead of {1,2} {1} stand for max 1 time a character \ means from here special characters (like .-_$ and so on) | means OR http://php.net/manual/en/function.ereg.php

« previous php.notes (#153583) next »