registration form validation


Create registration form. the file name is (registrationform.php)

<html>
<head>
<script>

function validation()
{
var x=document.forms[“myform”][“fname”].value;
if (x==null || x==””)
{
alert(“Enter the fist name”);
return false;
}
var x=document.forms[“myform”][“lname”].value;
if (x==null || x==””)
{
alert(“Enter the last name”);
return false;
}
var x=document.forms[“myform”][“email”].value;
var atpos=x.indexOf(“@”);
var dotpos=x.lastIndexOf(“.”);
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert(“Not a valid e-mail address”);
return false;
}
var x=document.forms[“myform”][“password”].value;
if(x==null || x==””)
{
alert(“Enter the password”);
return false;
}
var x=document.forms[“myform”][“cpassword”].value;
if(x==null || x==””)
{
alert(“Enter the cpassword”);
return false;
}
var x=document.forms[“myform”][“education”].value;
if(x==null || x==””)
{
alert(“enter the education”)
return false;
}
var x = document.myform.pho.value;
if (x==null || x==””)
{
alert(“Phone no. cannot be left blank”);
return false;
}

if(isNaN(x)|| x.indexOf(” “)!=-1)
{
alert(“Enter numeric value”);
return false;
}
if (x.length > 10)
{
alert(“enter 10 characters”);
return false;
}

}

</script>
</head>
<body>

<form action=”submitdata1.php” method=”post” name=”myform” onsubmit=”return validation()” >
<table border=’1′ bgcolor=#999>
<tr> <td>First_Name</td><td> <input type =”text” name=”fname” /></td></tr>
<tr><td>Last_Name</td><td> <input type =”text” name=”lname” /></td></tr>
<tr><td>Email </td><td><input type =”text” name =”email” /></td></tr>
<tr><td> Password </td><td><input type =”text” name=”password” /></td></tr>
<tr><td>Cpassword </td><td><input type = “text” name= “cpassword”/></td></tr>
<tr><td>Education</td><td> <input type=”text” name=”education” /></td></tr>
<tr><td>PhoNumber</td><td><input  type=”text” name=”pho” /></td></tr>
<td><input type =”submit” name=”submit”  value=”submit”></td></tr>
</form>
</table>

</body>
</html>

create php code for submit form in database. the file name is (submitdata.php)

<?php
if(isset($_POST[“submit”]))
{
echo ” ” .$_POST[“fname”] . “<br/>”;
echo ” ” .$_POST[“lname”] . “<br/>”;
echo ” ” .$_POST[“email”] . “<br/>”;
echo ” ” . $_POST[“password”] . “<br/>”;
echo ” ” . $_POST[“cpassword”] . “<br/>”;
echo ” ” . $_POST[“pho”] . “<br/>”;

$FirstName = $_POST[“fname”];
$LastName = $_POST[“lname”];
$Email = $_POST[“email”];
$Password = $_POST[“password”];
$Cpassword = $_POST[“cpassword”];
$Education = $_POST[“education”];
$Phonumber = $_POST[“pho”];

$con=mysql_connect(“localhost”,”root”,””) or die(“connection failed”);
mysql_select_db(“ratnesh”,$con) or die(mysql_error(!$con));
$query = “Insert Into data (FirstName,LastName,Email,Password,
Cpassword,Education,PhoNumber) values (‘$FirstName’,’$LastName’,’$Email’,
‘$Password’,’$Cpassword’,’$Education’,’$Phonumber’)”;
$result= mysql_query($query);
}
?>

Leave a comment