Advertisement
touhid_xml

Form validation in PHP

Apr 7th, 2015
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Small PHP Form Validation</title>
  5.     <style>
  6.         .error{color:red}
  7.     </style>
  8. </head>
  9. <body>
  10. <?php
  11. if(isset($_POST['form1'])) {
  12.     $u_name = $_POST['u_name'];
  13.     $u_email = $_POST['u_email'];
  14.     $u_username = $_POST['u_username'];
  15.     $u_password = $_POST['u_password'];
  16.  
  17.     $valid=1;
  18.  
  19.     if(empty($u_name)) {
  20.         echo "<div class='error'>Your name Can't be Empty</div><br>";
  21.         $valid = 0;
  22.     }
  23.     if(empty($u_email)) {
  24.         echo "<div class='error'>Your Email Can not be Empty</div><br>";
  25.         $valid = 0;
  26.     }
  27.     if(empty($u_username)) {
  28.         echo "<div class='error'>Username Can not be Empty</div><br>";
  29.         $valid = 0;
  30.     }
  31.     if(empty($u_password)) {
  32.         echo "<div class='error'>Password Can not be Empty</div><br>";
  33.         $valid = 0;
  34.     }
  35.  
  36.     if(!(preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $u_email))) {
  37.         echo "<div class='error'>Email Not Valid. Please Enter Valid Email Address</div>";
  38.         $valid = 0;
  39.     }
  40.     if($valid == 1) {
  41.         echo "Success";
  42.     }
  43.     else {
  44.         echo "Wrong";
  45.     }
  46. }
  47. else{?>
  48. <form action="" method="post">
  49.     <table>
  50.         <tr>
  51.             <td>Name: </td>
  52.             <td><input type="text" name="u_name" placeholder="Full Name"  /></td>
  53.         </tr>
  54.         <tr>
  55.             <td>Email Address: </td>
  56.             <td><input type="text" name="u_email" placeholder="Email Address" ></td>
  57.         </tr>
  58.         <tr>
  59.             <td>Username: </td>
  60.             <td><input type="text" name="u_username" placeholder="Username" /></td>
  61.         </tr>
  62.         <tr>
  63.             <td>Password: </td>
  64.             <td><input type="password" name="u_password" placeholder="Password" /></td>
  65.         </tr>
  66.         <tr>
  67.             <td><input type="submit" value="SEND" name="form1"/></td>
  68.         </tr>
  69.     </table>
  70.     <body>
  71. </html>
  72.  
  73. <?php
  74. }
  75.  
  76. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement