Need Help checking for "@" and "." in email address

I need to check the email address for an “@” and “.” and if they are not present then kick out an error message. I have tried several different ways to get this to work with no luck. I want to use STRPOS() to make it work though. Can anyone help solve this problem? It is at the comment “Validate and process the email address”. Thanks!

[php]<?php
if (isset($_POST[‘action’])) {
$action = $_POST[‘action’];
} else {
$action = ‘start_app’;
}

switch ($action) {
case ‘start_app’:
$message = ‘Enter some data and click on the Submit button.’;
break;
case ‘process_data’:
$name = $_POST[‘name’];
$email = $_POST[‘email’];
$phone = $_POST[‘phone’];

    /*************************************************
     * validate and process the name
     ************************************************/
    if (empty($name)){
		$message = 'You must enter a name.';
		break;
	}
	
	//capitalize the first letters only
	$name = strtolower($name);
	$name = ucwords($name);
	
	//get first name from complete name
	$i1 = strpos($name, ' ');
	if ($i1 === false) {
		$first_name = $name;	
	} else {
		$first_name = substr($name, 0, $i1);
		
		//check for second space
		$i2 = strpos($name, ' ', $i1+1);
		if ($i2 === false) {
			$last_name = substr($name, $i1+1);
		} else {
			$len = $i2 - $i1;
			$middle_name = substr($name, $i1+1, $len);
			$last_name = substr($name, $i2);	
		}
		
	}

    /*************************************************
     * validate and process the email address
     ************************************************/
    // 1. make sure the user enters an email
	if (empty($email)){
		$message = 'You must enter an email address.';
		break;
	}
	// 2. make sure the email address has at least one @ sign and one dot character
	$spos = '@';
	$i = strpos($email, $spos);
	if ($i === false) {
	$message = 'Not a valid email address, please try again.';
	}
    
     //remove common formatting from phone number
	$phone = str_replace('-','', $phone);
	$phone = str_replace(' ','',$phone);
	$phone = str_replace('.','',$phone);
	
    /*************************************************
     * validate and process the phone number
     ************************************************/
    if (strlen($phone) != 10) {
		$message = 'The phone number must have at least ten digits.';
		break;	
	} else {
		$part1 = substr($phone, 0, 3);
		$part2 = substr($phone, 3, 3);
		$part3 = substr($phone, 6);
			
	} $phone = $part1. '-' . $part2. '-' . $part3;
	
    /*************************************************
     * Display the validation message
     ************************************************/
    $message = "Hello $first_name, \n\n".
			   "Thank you for entering this data:\n\n".
			   "Name: $first_name $middle_name $last_name\n".
			   "Email: $email\n".
			   "Phone: $phone\n";

    break;

}
include ‘string_tester.php’;
?>[/php]

Sometimes google is your best friend

Example one here http://php.net/manual/en/filter.examples.validation.php

[php]<?php
$email_a = ‘[email protected]’;
$email_b = ‘bogus’;

if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
echo “This (email_a) email address is considered valid.”;
}
if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
echo “This (email_b) email address is considered valid.”;
}
?>[/php]

My little function:

[php]
function isEmail($email){
return preg_match(’/^\S+@[\w\d.-]{2,}.[\w]{2,6}$/iU’, $email) ? TRUE : FALSE;
}
[/php]

There is a built in email validation tool just use that. see my previous post.

Sponsor our Newsletter | Privacy Policy | Terms of Service