Tuesday 14 April 2015

PHP FORM VALIDATION

(1)write code for  form.html

<form action="validation.php"  method="post">

Name     <input type="text"  name="name">

Contact  <input type="text"  name="contact">

Email    <input type="text" name="email">

<input type="submit"    name="submit"  value="submit"  >

</form>


(2)write code for validation.php:

<?php


$msg=array();






if(empty($_POST['email']))


{


$msg="enter your email";

}


$regex = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$^";


if(!preg_match( $regex, $_POST['email'] ))


{

$msg="enter your valid email";



}




if(empty($_POST['contact']))

{



$msg="enter your contact";



}

if(strlen($_POST['contact'])>10)

{


$msg="accept only 10 digit";


}



if(!is_numeric($_POST['contact']))

{


$msg="accept only numbers only";


}



if(empty($_POST['name']))

{


$msg="enter your name";



}



if(empty($msg))

{


echo "successful";

}

else

{

echo $msg;


}


?>

No comments:

Post a Comment