php – what is wrong with my email id validation code ? my regular expression works fine but the else block is not being executed at all-ThrowExceptions

Exception or error:

here in my code , name validation and also the contact validation is working fine , but the email id validation part is showing me “email is required” i.e its executing only the if-block of the email validation code .
can some one help me out in specifying the mistake i have done unknowingly.

this is my forms1.css file

div.leftone
{
	height: 450px;
	width: 450px;
	position: relative;
	background: #2F4F4F;
	top: 80px;
	left: 200px;
}
div.one
{
	background: #FFFACD;
	height: 450px;
	width: 450px;
	display: grid;
	
	
	position: relative;
	top: -370px;
	left: 650px;

}
div.two{
	
	text-align: center;
}
div.three  {
	text-shadow: 0px 5px 8px black;
	font-size: 20px
}
div.four
{
	font-size: 20px;
	display: inline-block;
	font-family: sans-serif;

}
input , label {
	float: left; /* pushes both to left i.e align left types */
}
label
{
	width: 180px; /* gives a 180 width */
	clear: left;
	text-align: left;
}

input
{
	margin-right: 20px;
	margin-left: 20px;
}
div.five
{
	float: left;
	font-size: 15px;
	
}
span.error
{
	font-size: 5spx;
	color: rgb(250, 250 ,100);
}
 <!-- form1.php -->

<?php
	include('form_process.php');
?>


<!DOCTYPE html>
<html>
<head>
	<title><img src="">Welcome</title>
	<link rel="stylesheet" type="text/css" href="/css/forms1.css">
</head>
<body>
	<div class="leftone">
		
	</div>
	<div class="one">
		<form action="<?= $_SERVER['PHP_SELF']; ?>" method="post">
			<div class="two">
				<div class="three"><h3>Dont have an account create one now!! <br />For free!!!</h3></div>
				<div class="four">
					<p>
						<label>Name:</label>
						<input type="text" name="name"  id="name" placeholder="Name" autofocus>
						<span class="erros"><?= $name_err ?></span>
						<br /> <br />
					</p>
					<p>
						<label>Emailid:&nbsp;</label>
						<input type="text" name="emailid" required id="emailid" placeholder="Emailid">
						<span class="erros"><?= $email_err ?></span>
						<br /><br />
					</p>
					<p>
						<label>Contact Number:</label>
						<input type="text" name="num" required id="num" placeholder="Mobile num">
						<span class="erros"><?= $contact_err ?></span>
						<br /><br />
					</p>
					<input type="submit" name="submit" onclick="vali()">
					<input type="reset" name="reset">
					<div class="five">
						<br />
						<p>Already have an account?<a href="">Login</a></p>
					</div>
				</div>
			</div>
		</form>
	</div>
</body>
</html>

while this is my form_process.php

`<!-- define variables and set to empty values -->
<?php
 $name_err=$email_err=$contact_err="";
 $name=$email=$contact="";

 function test_input($data)
{
    $data=trim($data);
    $data=stripslashes($data);
    $data=htmlspecialchars($data);
    return $data;
}

if($_SERVER["REQUEST_METHOD"]=="POST")
{
if(empty($_POST["name"]))
{
    $name_err="Name is required";
}
else
{
    $name = test_input($_POST["name"]);
    // check if name only has letters and blank space 
    if(!preg_match("/^[a-zA-Z ]*$/",$name))
    {
        $name_err="only letters are white space allowed";
    }
}

if(empty($_POST["email"]))
{
    $email_err="email is required";
}
else
{
    $email = test_input($_POST["email"]);

    if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
    {
        $email_err="invalid email id format ";
    }
}

if(empty($_POST["num"]))
{
    $contact_err="number is required";
}
else
{
    $contact = test_input($_POST["num"]);
    // check if name only has letters and blank space 
    if(!preg_match("/^[\d]{10}$/",$contact))
    {
        $contact_err="invalid mobile number format ";
    }
}



}
?>

`

can some one tell me where did i go wrong? even my color for the span tag isnt working fine

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *