Can anyone tell me what wrong i am doing here ? Error shows only if the First name is blank, for the rest (e.g lastname/email/body) it's not working. EMail validation also not working.
$error = "";
if (empty($fanme)) {
$error = "First name must not be empty !";
}
elseif (empty($lname)) {
$error = "Last name must not be empty !";
}
elseif (empty($email)) {
$error = "email must not be empty !";
}
elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error = "Invalid Email Address !";
}
elseif (empty($body)) {
$error = "Message field not be empty !";
}
else {
$msg = "ok";
}
}
if (isset($error)) {
echo "<span style='color:red'>$error</span>";
}
if (isset($msg)) {
echo "<span style='color:green'>$msg</span>";
}
It looks like you misspelled "$fname" in line 2 of your code above.
Related
If I have some basic form validations, (just using empty() for simplicities sake) and want to put those error messages into an array, how would I achieve this?
$errors = array();
$response = array();
if(empty($_POST['name'])) {
$errors['name'] = "Name required";
}
if(empty($_POST['email'])) {
$errors['email'] = "Email required";
}
$response['errors'] = $errors;
if(!empty($errors)) {
$response['success'] = false;
$response['message'] = "fail";
} else {
$response['success'] = true;
$response['message'] = "<div class='alert alert-success'>Success</div>";
}
echo json_encode($response);
}
$message = [];
if(empty($_POST['name'])) {
array_push($message , "Name required <br />");
}
if(empty($_POST['email'])) {
array_push($message , "Email required <br />");
}
if(!empty($message)) {
foreach ( $message as $str)
echo "<div class='alert alert-danger'>" . $str . "</div>";
} else {
// success
}
This is my code to validate email, it works great but I want if the email does not end with a .mil it says invalid email
if (isset($_POST['update_email'])) {
$email = escape($_POST['email']);
if (empty($email)) {
$errors[] = "Email Address Required.";
}
if (email_exists($email)) {
$errors[] = "Email Address in use.";
}
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$errors[] = "Invalid Email Address";
}
if (! empty($errors)) {
echo validation_errors($errors[0]);
} else {
}
Thanks.
Add a regular expression conditional statement
if (!preg_match('/\.mil$/', $email) {
$errors[] = "Invalid Email Address";`
}
Or perhaps you can build this logic inside email_exists.
I use am using bootstrap.. I need to display errors 1 at a time. the following code post the errors in different rows and it consumes a whole page.
if there is an issue in the first field only post that error before continuing to the next field instead of posting all fields at once.
function validate_registration(){
$errors = [];
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$email = clean($_POST['email']);
$user_name = clean($_POST['user_name']);
$birthdate = clean($_POST['birthdate']);
$country = clean($_POST['country']);
$password = clean($_POST['password']);
$confirm_password = clean($_POST['confirm_password']);
if (empty($email)) {
$errors[] = "Email Address Required.";
}
if (email_exists($email)) {
$errors[] = "Email Address in use.";
}
if (empty($user_name)) {
$errors[] = "Username Required.";
}
if (strlen($user_name) < $min) {
$errors[] = "Username is to short.";
}
if (user_exists($user_name)) {
$errors[] = "Username Taken.";
}
if (empty($birthdate)) {
$errors[] = "Birthdate Required.";
}
if (empty($country)) {
$errors[] = "Country Required.";
}
if (empty($password)) {
$errors[] = "Password Required.";
}
if ($password !== $confirm_password) {
$errors[] = "Your password fields do not match";
}
if (! empty($errors)) {
foreach ($errors as $error) {
echo validation_errors($error);
}
} else {
if (register_user($email, $user_name, $birthdate, $country, $password)) {
set_message('<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
Please check your email for account Information.</div>');
redirect("../account/login.php");
}
}
}
}
You can also do the same by keeping your existing code.
Just replace this code:-
if (!empty($errors)) {
echo validation_errors($errors[0]);
}
You can also get the same functionality by if else if way. declare $error as a string.
$error = '';
if(empty($email)) {
$error = "Email Address Required.";
}
else if (email_exists($email)) {
$error = "Email Address in use.";
}
else if (empty($user_name)) {
$error = "Username Required.";
}
else if (strlen($user_name) < $min) {
$error = "Username is to short.";
}
else if (user_exists($user_name)) {
$error = "Username Taken.";
}
else if (empty($birthdate)) {
$error = "Birthdate Required.";
}
else if (empty($country)) {
$error = "Country Required.";
}
else if (empty($password)) {
$error = "Password Required.";
}
else if ($password !== $confirm_password) {
$error = "Your password fields do not match";
}
if (!empty($error)) {
echo validation_errors($error);
} else {
if (register_user($email, $user_name, $birthdate, $country, $password)) {
set_message('<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
Please check your email for account Information.</div>');
redirect("../account/login.php");
}
}
For Bootstrap Validations, Just visit this link:-
Hope it will help you :)
UPDATED:
As per your comments, if you want to print one error at a time than you need to use ELSEIF instead of IF like:
if (empty($email)) {
$errors[] = "Email Address Required.";
}
elseif (email_exists($email)) {
$errors[] = "Email Address in use.";
}
...
Why are not using jquery validation for client side validation
and then use server side validation too but display it as all
error in loop instead of inline .
Because client as well as server side validation is useful
I m trying to validate only numbers in php but it is displaying error message.I want user to enter a valid mobile number,batch where only 4 numbers as to be entered. No characters should be entered.Please tell me whats the error in the code.
Here is the code
if (isset($_POST['submit'])) {
$error = "";
if (!empty($_POST['name'])) {
$name = $_POST['name'];
} else {
$error .= "You didn't type in your name. <br />";
}
if (!empty($_POST['email'])) {
$email = $_POST['email'];
if (!preg_match("/^[_a-z0-9]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){
$error .= "The e-mail address you entered is not valid. <br/>";
}
} else {
$error .= "You didn't type in an e-mail address. <br />";
}
if (!empty($_POST['batch'])) {
$batch = $_POST['batch'];
}
if (!preg_match('/^[0-9]+$/', $batch)) {
$error .= "Enter a Valid Number. <br/>";
}
else {
$error .= "You didn't type batch. <br />";
}
if(($_POST['code']) == $_SESSION['code']) {
$code = $_POST['code'];
} else {
$error .= "The captcha code you entered does not match. Please try again. <br />";
}
if (!empty($_POST['mobile'])) {
$mobile = $_POST['mobile'];
}
if (!preg_match('/^[0-9]+$/', $mobile)){
$error .= "Enter A Valid Number. <br/>";
}
else {
$error .= "You didn't type your Mobile Number. <br />";
}
(!preg_match('/^[0-9]{4}$/', $batch)
Use this if you want to validate for only 4 numbers.
//validate email
if(empty($email)){
$mailErr = "<div class='errors'>Email can not be empty.</div>";
}else{
if(!(preg_match("/^[a-zA-Z0-9\-_]+(\.[a-zA-Z0-9\-_]+)*#[a-z0-9]+(\-[a-z0-9]+)*(\.[a-z0-9]+(\-[a-z0-9]+)*)*\.[a-z]{2,4}$/", $email))){
$mailErr = "<div class='errors'>Please input a valid email address.</div>";
}
}
//validate message
if (empty($message)) {
$messageErr = "<div class='errors'>Message can not be empty.</div>";
}
if both validation are true then send mail. please help me. How can I write this condition. I want if both conditions are fulfill then send the mail.
// Validate email
if (empty($email)) {
$mailErr = "<div class='errors'>Email can not be empty.</div>";
} else if (!(preg_match("/^[a-zA-Z0-9\-_]+(\.[a-zA-Z0-9\-_]+)*#[a-z0-9]+(\-[a-z0-9]+)*(\.[a-z0-9]+(\-[a-z0-9]+)*)*\.[a-z]{2,4}$/", $email))){
$mailErr = "<div class='errors'>Please input a valid email address.</div>";
} else {
$valid_email = true;
}
// Validate message
if (empty($message)) {
$messageErr = "<div class='errors'>Message can not be empty.</div>";
}else{
$valid_message = true;
}
if ($valid_email && $valid_message) {
// Send the email
}
There is a short form to validate Email-Adresses using FILTER_VALIDATE_EMAIL and you can just safe a variable, which contains an error status.
$error = false;
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$mailErr = "<div class='errors'>Please input a valid email address.</div>";
$error = true;
}
if (empty($message)) {
$messageErr = "<div class='errors'>Message can not be empty.</div>";
$error = true;
}
if(!$error) {
//Send message here
}
You can also use a short form (but not checking both at the same time) without using a status variable:
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$mailErr = "<div class='errors'>Please input a valid email address.</div>";
} else if (empty($message)) {
$messageErr = "<div class='errors'>Message can not be empty.</div>";
} else {
//Send message here
}