skip to Main Content

I am doing email validation for admin registration using JavaScript and save the data to database using PHP. Supposedly, the registration is done only if the email is valid. But when the email evaluates to invalid, the PHP code still run. How do I do it so that when the email is invalid, the PHP won’t run.

Below is the PHP code to save data to database:

<?php
include('connection.php');

if(isset($_POST['saveBtn']))
{
    $name    = $_POST['name'];
    $ic      = $_POST['ic'];
    $email   = $_POST['email'];
    $pass    = $_POST['pass'];
    $dob     = $_POST['dob'];
    $contact = $_POST['contact'];
    $gender  = $_POST['gender'];
    $des     = $_POST['des'];
    $address = $_POST['address'];


    // Check if data exist
    $check = "SELECT * FROM admin WHERE admEmail = '".$email."' AND admPassword = '".$pass."'";
    if(mysqli_num_rows(mysqli_query($connect,$check)) > 0)
    {
        ?>
        <script>
            alert('This email and password already registered!');
        </script>
        <?php
    }
    else
    {
        $insert = "INSERT INTO admin (admName, admIC, admEmail, admPassword, admDOB, admContact, admGender, admDesignation, admAddress, admDateJoin) VALUES ('".$name."', '".$ic."', '".$email."', '".$pass."', '".$dob."', '".$contact."', '".$gender."', '".$des."', '".$address."', NOW())";
    
        if(mysqli_query($connect, $insert))
        {
            ?>
            <script>
                alert('Insertion Successful!');

                window.close();
                window.opener.location.reload();
            </script>
            <?php
        }
        else
        {
            ?>
            <script>
                alert('Insertion Failed. Try Again!');
            </script>
            <?php
        }
    }
}
?>

Below is the JS:

function validateEmail() {

            var email = document.addAdminForm.email.value;
            var validRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)*$/;

            if (email.match(validRegex))
            {
                alert("Valid email address!");
                
                return true;
            }
            else
            {
                document.getElementById("email_error").innerHTML = "Invalid email";

                document.addAdminForm.email.focus();

                return false;
            }

        }

Below is the partial HTML form:

<form class="w-100" name="addAdminForm" method="POST" onsubmit="validateEmail(this)" action="add_admin.php">
    <div class="row">
        <div class="col form-group">
            <!-- <label for="email">Email</label> -->
            <input type="text" class="form-control" name="email" placeholder="Email" required>
            <span class="error email_error" id="email_error"></span>
        </div>
    <div class="float-right">
        <input type="submit" class="btn button_primary" value="Save" name="saveBtn">
    </div>
</form>

I expect PHP run when validation is true

2

Answers


  1. add this:

    onsubmit="return validateEmail(this)"
    
    Login or Signup to reply.
  2. change your JS code to:

    var validRegex = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search