Server Side Form Validation (Registration Form)
Submitted by GeePee on Monday, June 1, 2015 - 22:24.
Validating a form using a JavaScript gives convenience to your visitors by avoiding page reload and other features that we discuss on our previous tutorial called Validate Login Page Using JavaScript.
But validating a form using JavaScript alone is not safe. One reason is if the JavaScript is not enabled in the web browser. This will bypass all the validation that you have defined in your code.
So, to avoid this kind of problem, all you need to do is validate it on server side.
The following are the example of a registration form with validation using PHP.
validate_registration.php
Now our validation.php script
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Registration Form</title>
- </head>
- <body>
- <?php include("validation.php"); ?>
- <form method="post" action="" name="form">
- <table width="510" border="0">
- <tr>
- <td> </td>
- <td>Registration Form</td>
- <td> </td>
- </tr>
- <tr>
- <td>Username :</td>
- <td><input type="text" name="username" value="<?php echo $val_username; ?>" /></td>
- <td><?php echo $err_username; ?></td>
- </tr>
- <tr>
- <td>Password :</td>
- <td><input type="password" name="password" value="<?php echo $val_password; ?>" /></td>
- <td><?php echo $err_password; ?></td>
- </tr>
- <tr>
- <td>Name :</td>
- <td><input type="text" name="name" value="<?php echo $val_name; ?>" /></td>
- <td><?php echo $err_name; ?></td>
- </tr>
- <tr>
- <td>Email : </td>
- <td><input type="text" name="email" value="<?php echo $val_email; ?>" /></td>
- <td><?php echo $err_email; ?></td>
- </tr>
- <tr>
- <td> </td>
- <td><input type="submit" name="button" id="button" value="Submit" /></td>
- <td> </td>
- </tr>
- </table>
- </form>
- </body>
- </html>
- <?php
- if($_POST)
- {
- $username = $_POST['username'];
- $password = $_POST['password'];
- $name = $_POST['name'];
- $email = $_POST['email'];
- // Full Name
- $val_name = $name;
- }else{
- $err_name='Please enter valid Name.';
- }
- // Email
- if (preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email)) {
- $val_email = $email;
- }else{
- $err_email = 'Please enter valid Email.';
- }
- // Usename min 2 char max 20 char
- $val_username = $username;
- }else{
- $err_username = 'Please enter valid Username (minimum 3 characters)';
- }
- // Password min 6 char max 20 char
- $val_password = $password;
- }else{
- $err_password = 'Please enter valid Password (minimum 6 characters)';
- }
- }else{ }
- }
- ?>
Add new comment
- 299 views