Reputation: 75
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.
check.php
<?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 ($email==NULL) {
echo 'Enter an email';
} elseif (strlen($email)<6) {
echo 'Please enter a valid email';
} elseif (str($email).indexOf('@') == -1) {
echo 'Please enter a valid email';
} else {
if ($check_num_rows==0) {
echo 'Email valid';
} elseif ($check_num_rows==1) {
echo 'Email already registered';
}
}
register.php
<script type="text/javascript" src="/jquery-3.2.1.min.js"></script>
<script>
$(document).ready(function() {
$('#feedback_email').load('/check.php').show();
$('#email').keyup(function() {
$.post('/check.php', { email: form.email.value },
function(result) {
$('#feedback_email').html(result).show;
});
});
});
</script>
<form action="register.php" method="post" name="form">
<input id="email" type="text" name="email" placeholder="Email Address" maxlength="60" value="<?php if (isset($trimmed['email'])) echo $trimmed['email']; ?>">
<div id="feedback_email"></div>
<p><center><input type="submit" name="submit" value="Register"></center>
</form>
EDIT: I do have a built in filter for the php back-end validation. However, I'm trying to do a front-end validation as well through these codes. If anyone knows how to fix my script please post. I'm receiving error
Call to undefined function str() in /check.php on line 13
Line 13
} elseif (str($email).indexOf('@') == -1) {
Upvotes: 1
Views: 166
Reputation: 11
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 protected]';
if(isValidEmailAddr($test) == true){
echo "good to go";
} else { echo "try again babe!" }
Upvotes: 0
Reputation: 605
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);
}
Upvotes: 1
Reputation: 318182
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) { ...
Upvotes: 2
Reputation: 686
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
Upvotes: 0