PHP Simple Form Validation

0 votes

I want a form validation like the following.

  1. Name: [required, min length: 2, max length: 255]
  2. Email: [required, min length: 3, max length: 255, valid email format]
  3. Date of Birth: [optional, format: dd/mm/yyyy]

My code so far:

index.php

<form method="post" action="confirm.php">
Name:<input type="text" name="name" />
email:<input type="text" name="email" />
DOB:<input type="date" name="dob" />
<input type="submit" value="submit" />
</form>

and

confirm.php

<?php

$name = $_POST['$name'];
$email = $_POST['$email'];
$dob = $_POST['$dob'];

$namelen = strlen($email);
$emaillen = strlen($email);
$max = 255;
$minname = 2;
$minemail = 3;

if($namelen<$minname){
    echo"name must be at least 2 characters";
}
elseif($namelen>$max){
    echo"name must be less than 255 characters";
}

if(empty($name)){
    echo"name is required";
}
else{
    continue;
}

if($emaillen<$minemail){
    echo"email must be at least 3 characters";
}
elseif($emaillen>$max){
    echo"email must be less than 255 characters";
}

if(filter_var($email, FILTER_VALIDATE_EMAIL)){
    continue;
}
else{
    echo"invalid email";
}

if(empty($email)){
    echo"email cannot be left empty";
}
else{
    continue;
}

?>

Can someone please help me do this?

 

Jun 26, 2022 in PHP by narikkadan
• 86,360 points
• 1,959 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

You have some conditions like this:

if(condition == true) {
    continue;
} else {
    // do something
}

It's actually not necessary, and you could change it to:

if(!condition) {
    // do something
}

The modified code:

if(!empty($_POST)) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $dob = $_POST['dob'];

    $namelen = strlen($name);
    $emaillen = strlen($email);
    $max = 255;
    $minname = 2;
    $minemail = 3;

    if($namelen < $minname){
        $errors[] = "name must be at least 2 characters";
    } elseif($namelen > $max){
        $errors[] = "name must be less than 255 characters";
    }

    if($emaillen < $minemail){
        $errors[] = "email must be at least 3 characters";
    } elseif($emaillen > $max){
        $errors[] = "email must be less than 255 characters";
    }

    if(empty($name)){
        $errors[] = "name is required";
    }

    if(empty($email)){
        $errors[] = "email cannot be left empty";
    }

    if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
        $errors[] = "invalid email";
    }

    echo "<ul>";
    foreach ($errors as $error) {
        echo "<li>$error</li>";
    }
    echo "</ul>";

}

I hope this helps you.

answered Jun 27, 2022 by Kithuzzz
• 38,000 points

edited Mar 5, 2025

Related Questions In PHP

0 votes
1 answer

Complete PHP form with proper validation and syntax

Hey @kartik, It's quite simple to have php ...READ MORE

answered Feb 19, 2020 in PHP by Niroj
• 82,800 points
• 4,529 views
0 votes
0 answers

PHP Simple Search Form

Since I'm new to PHP, I'd like ...READ MORE

Aug 8, 2022 in PHP by Kithuzzz
• 38,000 points
• 1,410 views
0 votes
1 answer

What are the vulnerability related to PHP Form?

Hii, The $_SERVER["PHP_SELF"] variable can be used by ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,800 points
• 4,999 views
0 votes
1 answer

How can we avoid my php form from hacking?

Hii @kartik, If you want to know php ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,800 points
• 4,002 views
0 votes
1 answer

How to Validate Form Data With PHP?

Hey @kartik, The first thing we will do ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,800 points
• 5,258 views
0 votes
1 answer

How to validate E-mail and URL of Php form?

hey, The code below shows a simple way ...READ MORE

answered Feb 13, 2020 in PHP by manish
• 3,844 views
0 votes
0 answers

How to validate an Email in PHP?

How can I validate the input value ...READ MORE

Jun 16, 2022 in PHP by narikkadan
• 86,360 points
• 1,202 views
0 votes
0 answers
+1 vote
2 answers

Scp Php files into server using gradle

Tru something like this: plugins { id ...READ MORE

answered Oct 11, 2018 in DevOps & Agile by lina
• 8,220 points
• 3,541 views
0 votes
1 answer

How do I create folder under an Amazon S3 bucket through PHP API?

Of Course, it is possible to create ...READ MORE

answered Apr 24, 2018 in AWS by anonymous
• 14,017 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP