Day 13: Form validation client and server side.
Is it better to validate form of our website both client and server side. Both have their own advantages.
In client side validation we use JavaScript so it’s faster than the server side validation but sometimes when JavaScript disabled on browser then JavaScript doesn’t work then server side validation will work.
Create below mentioned table in your database.
create table user (id int primary key auto_increment, name varchar(50), email varchar(50), gender varchar(1), phone varchar(10), address varchar(50))
Program 40: Working with form validation (client and server side).
File 1. connect.php :
$con = mysql_connect(“localhost”,”root”,””);
mysql_select_db(“mydb”,$con);
?>
File 2. register.js : Used for client side validation.
var frm = document.getElementById(“frmRegister”);
var err = “”;
var errDiv = document.getElementById(“msg”);
var name = frm.txtName.value;
if(name.trim() == “”) {
err = “*name is required.<br/>”;
}
var email = frm.txtEmail.value;
var expEmail = /^(([^<>()[\]\\.,;:\s@\”]+(\.[^<>()[\]\\.,;:\s@\”]+)*)|(\”.+\”))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if(email.trim()==””) {
err = err+”*email is required.<br/>”;
}
else if(!expEmail.test(email)) {
err = err+”*Invalid email<br/>”;
}
var password = frm.txtPassword.value;
if(password.trim()==””) {
err = err+”*Password required.<br/>”;
}
else if(password.length < 4) {
err = err+"*Password should be minimun 4 characters.<br/>";
}
var confPassword = frm.txtConfPassword.value;
if(confPassword!=password) {
err = err+"*Password and confirm password should be same.<br/>";
}
var phone = frm.txtPhone.value;
var expPhone = /^\d*$/;
if(phone!="" && !expPhone.test(phone)) {
err = err+"*Phone should be in integer.<br/>";
}
if(err.trim()!="") {
errDiv.innerHTML = err;
return false;
} else {
return true;
}
}
File 3. registration.php : Used for serve side validation and save the record in dabase.
require_once “connect.php”;
if(isset($_REQUEST[‘btnSubmit’])) {
$name = $_REQUEST[‘txtName’];
$email = $_REQUEST[‘txtEmail’];
$password = $_REQUEST[‘txtPassword’];
$confPassword = $_REQUEST[‘txtConfPassword’];
$gender = $_REQUEST[‘rdGender’];
$phone = $_REQUEST[‘txtPhone’];
$address = $_REQUEST[‘txtAddress’];
$msg = “”;
$expEmail =”/^(([^<>()[\]\\.,;:\s@\”]+(\.[^<>()[\]\\.,;:\s@\”]+)*)|(\”.+\”))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/”;
if(trim($name) == “”) {
$msg = “*name is required.<br/>”;
}
if(trim($email) == “”) {
$msg = $msg.”*email is required.<br/>”;
}
else if(!preg_match($expEmail,$email)) {
$msg = $msg.”*Invalid email<br/>”;
}
if(trim($password) == “”) {
$msg = $msg.”*Password required.<br/>”;
}
else if(strlen($password) < 4) { $msg = $msg."*Password should be minimun 4 characters.<br/>"; } if($confPassword != $password) { $msg = $msg."*Password and confirm password should be same.<br/>"; } $expPhone = "/^\d*$/"; if(!preg_match($expPhone, $phone)) { $msg = $msg."*Phone should be in integer.<br/>"; } if($msg == ""){ $query = "insert into user (name,email,gender,phone,address) values ('$name','$email','$gender','$phone','$address')"; if(mysql_query($query)){ echo "Record Saved!"; } else { echo "Unable to Save!"; } } } ?> <html> <head> <title>Registration</title> <script src="register.js"></script> </head> <body> <div id="msg" style="color:red"> <?php echo $msg; ?> </div> <form id="frmRegister" method="post" onSubmit="return validate()"> <table cellspacing="5px"> <tr> <td>Name*</td> <td><input type="text" value="<?php echo isset($_POST['txtName']) ? $_POST['txtName'] : ""; ?>" name="txtName" id="txtName"/></td> </tr> <tr> <td>Email*</td> <td><input type="text" value="<?php echo isset($_POST['txtEmail']) ? $_POST['txtEmail'] : ""; ?>" name="txtEmail" id="txtEmail"/></td> </tr> <tr> <td>Password*</td> <td><input type="password" name="txtPassword" id="txtPassword"/></td> </tr> <tr> <td>Confirm Password*</td> <td><input type="password" name="txtConfPassword" id="txtConfPassword"/></td> </tr> <tr> <td>Gender</td> <td> <input type="radio" <?php if($_POST['rdGender']=="m") echo "checked"; ?> name="rdGender" value="m" id="rdMale"/> Male <input type="radio" <?php if($_POST['rdGender']=="f") echo "checked"; ?> name="rdGender" value="f" id="rdFemale"/> Female </td> </tr> <tr> <td>Phone</td> <td><input type="text" maxlength="10" value="<?php echo isset($_POST['txtPhone']) ? $_POST['txtPhone'] : ""; ?>" name="txtPhone" id="txtPhone"></td> </tr> <tr> <td>Address</td> <td><textarea name="txtAddress" id="txtAddress"><?php echo isset($_POST['txtAddress']) ? $_POST['txtAddress'] : "";?></textarea></td> </tr> <tr> <td></td> <td> <input type="reset" name="btnReset" value="Reset"/> <input type="submit" name="btnSubmit" id="btnSubmit" value="Submit"/> </td> </tr> </table> </form> </body> </html>