Trying to get code to search if \'@\' symbol is present in typed-in email address and echo if symbol is not. Everything works fine without the searching for @ code.
chec
try this :-
function isValidEmailAddr ($email) {
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
return true;
} else {
return false;
}
}
usage :-
include('path/to/lib.php');
$test = 'email@foo.bar';
if(isValidEmailAddr($test) == true){
echo "good to go";
} else { echo "try again babe!" }
It will not only validate email address, but also sanitize it for unexpected characters:
$email = $_POST['email'];
$emailB = filter_var($email,
FILTER_SANITIZE_EMAIL);
if(filter_var($emailB,
FILTER_VALIDATE_EMAIL) === false ||
$emailB != $email ) { echo "This email adress
isn't valid!"; exit(0);
}
A much better approach to validating the emails, would be to use the built in filters intended for that sort of thing
<?php
include '/connect.php'; //connects to mysql
$email = mysqli_real_escape_string($connect, $_POST['email']);
$check = mysqli_query($connect, "SELECT email FROM users WHERE email='$email'");
$check_num_rows = mysqli_num_rows($check);
if ( filter_var($email, FILTER_VALIDATE_EMAIL) ) {
if ($check_num_rows==0) {
echo 'Email valid';
} elseif ($check_num_rows==1) {
echo 'Email already registered';
}
} else {
echo 'Please enter a valid email';
}
?>
The reason your code doesn't work, is because PHP doesn't have a str
or indexOf
method, you should be using something like strpos
instead
if (strpos($email, '@') === false) { ...
you can use default php email validation
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo("$email is a valid email address");
} else {
echo("$email is not a valid email address");
}
?>
you can also check this at https://eval.in/870776