Hi, i am doing an exercise on email validation and i met some problem. i am trying to validate an email address if there is an @ symbol in it. here's the code: <?php if(isset($_POST['name'])){ $name = ucwords($_POST['name']); echo "Hello ".$name."<br />"; } if(isset($_POST['email']{ [B][COLOR="blue"]if(ereg("@",$_POST['email'])){[/COLOR][/B] echo "Your email submission is ". $_POST['email']."<br />"; }else{ echo "Please enter an @ sign to your email"; } } ?> Code (markup): The error is at the bold blue part ( line 7). I wonder if i did anything wrong with the ereg. Is ereg function being deprecated in php? if i cannot use ereg, what else can be done? pls advise, thanks.
I just wrote this works fine, <?php $string = "glen.email.com"; $look = "/@/"; if(preg_match($look,$string)) { echo "Found it"; } else { echo "It's not there!"; } ?> PHP:
Yes, ereg function is being deprecated and should not be used, ever. You can use preg_match instead. I think the problem with your code is this: if(isset($_POST['email']{ Code (markup): You made a typo, you forgot to add "))"
i already got the "))" in the code. i will check preg_match. I just dunno why my script can't run and got an error at that part.
The script I wrote works fine please read my post. You shouldn't use that function anymore it's depreceated. Glen
For preg_match, i need to use "/@/", ( include the backslash?) but for ereg there is not a need for the backslash?
If your just checking if theirs the @ (at) character within the email string, you can just do (theirs no need for a regex): if (substr_count($email, '@') == 1) { //proceed... } PHP: However if your trying to validate it to ensure its in a valid email format, you can use a regex, but I don't recommend using sunnyverma1984's one as that has a few flaws (doesn't allow lowercase letters, doesn't accept subdomains, doesn't allow tld's such as co.uk, hasn't escaped/backslashed special characters, doesn't validate the whole string and so on.)
The regular expression library really helps when you don't have time to create your own regular expressions: http://regexlib.com Here is a function I use to check email address: function checkEmail($string) { return preg_match('/^[.\w-]+@([\w-]+\.)+[a-zA-Z]{2,6}$/', $string); } var_dump(checkEmail('alex.JOHNSON.Department-Outsource@gmail.co.uk.COM')); PHP: