Form validation client and server side using PHP

Form validation client and server side using PHP

In this Post We Will Explain About is Form validation client and server side using PHP With Example and Demo.Welcome on Live24u.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Differentiate between client side validation and server side validationExample

In this post we will show you Best way to implement Creating Client and Server-Side Form Validation, hear for Registration form with server And client side validationwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.




In javascript simple client side validation we use JavaScript so It is a simple faster than Like as a PHP the server side validation but and manytimes when JavaScript simple disabled on browser data then JavaScript simple doesn’t work then any server side like as a validation will work.

Also Read :   php application global config and settings - php superglobals

create table user (id int primary key auto_increment,
name varchar(50),
useremail varchar(50),
gender varchar(1),
mobile varchar(10),
locations varchar(50))

File 1. connect.php :

<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("mydb",$con);
?>

File 2. register.js :

Used for client side validation.

function validate() {
    var liveform_db = document.getElementById("myformRegister");
    var err = "";
    var errDiv = document.getElementById("livemessage");
    var name = liveform_db.userName.value;
    if (name.trim() == "") {
        err = " * name is required. < br / > ";
    }

    var useremail = liveform_db.userEmail.value;
    var liveEmail = /^(([^<>()[\]\\.,;:\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 (useremail.trim() == "") {
        err = err + " * useremail is required. < br / > ";
    } else if (!liveEmail.test(useremail)) {
        err = err + " * Invalid useremail < br / > ";
    }

    var password = liveform_db.userPassword.value;
    if (password.trim() == "") {
        err = err + " * Password required. < br / > ";
    } else if (password.length < 4) {
        err = err + "*Password required be minimun 4 characters.<br/>";
    }
    var confPassword = liveform_db.userConfPassword.value;
    if (confPassword != password) {
        err = err + "*Password and confirm password required be same.<br/>";
    }
    var mobile = liveform_db.userPhone.value;
    var latestPhone = /^\d*$/;
    if (mobile != "" && !latestPhone.test(mobile)) {
        err = err + "*Phone required be in integer.<br/>";
    }
    if (err.trim() != "") {
        errDiv.innerHTML = err;
        return false;
    } else {
        return true;
    }
}




Also Read :   Ionic checkbox check all and uncheck all checkboxes Angularjs

File 3. registration.php :

Used for simple server side Like as a PHP validation and save the Databased record in table.

<?php
require_once "connect.php";
if(isset($_REQUEST['btnSubmit'])) {
$name = $_REQUEST['userName'];
$useremail = $_REQUEST['userEmail'];
$password = $_REQUEST['userPassword'];
$confPassword = $_REQUEST['userConfPassword'];
$gender = $_REQUEST['userGender'];
$mobile = $_REQUEST['userPhone'];
$locations = $_REQUEST['userAddress'];
$livemessage = "";
$liveEmail ="/^(([^<>()[\]\\.,;:\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) == "") {
$livemessage = "*name is required.<br/>";
}
if(trim($useremail) == "") {
$livemessage = $livemessage."*useremail is required.<br/>";
}
else if(!preg_match($liveEmail,$useremail)) {
$livemessage = $livemessage."*Invalid useremail<br/>";
}
if(trim($password) == "") {
$livemessage = $livemessage."*Password required.<br/>";
}
else if(strlen($password) < 4) { $livemessage = $livemessage."*Password required be minimun 4 characters.<br/>"; } if($confPassword != $password) { $livemessage = $livemessage."*Password and confirm password required be same.<br/>"; } $latestPhone = "/^\d*$/"; if(!preg_match($latestPhone, $mobile)) { $livemessage = $livemessage."*Phone required be in integer.<br/>"; } if($livemessage == ""){ $query = "insert into user (name,useremail,gender,mobile,locations) values ('$name','$useremail','$gender','$mobile','$locations')"; if(mysql_query($query)){ echo "Live Record Saved!"; } else { echo "Live Unable to Save!"; } } } ?>
    <html>

    <head>
        <title>Live24u.com Registration</title>
        <script src="register.js"></script>
    </head>

    <body>
        <div id="livemessage" style="color:red">
            <?php echo $livemessage; ?> </div>
        <form id="myformRegister" method="post" onSubmit="return validate()">
            <table cellspacing="5px">
                <tr>
                    <td>Name*</td>
                    <td><input type="text" value="<?php echo isset($_POST['userName']) ? $_POST['userName'] : " "; ?>" name="userName" id="userName" /></td>
                </tr>
                <tr>
                    <td>Email*</td>
                    <td><input type="text" value="<?php echo isset($_POST['userEmail']) ? $_POST['userEmail'] : " "; ?>" name="userEmail" id="userEmail" /></td>
                </tr>
                <tr>
                    <td>USER Password*</td>
                    <td><input type="password" name="userPassword" id="userPassword" /></td>
                </tr>
                <tr>
                    <td>USER Confirm Password*</td>
                    <td><input type="password" name="userConfPassword" id="userConfPassword" /></td>
                </tr>
                <tr>
                    <td>USER Gender</td>
                    <td> <input type="radio" <?php if($_POST[ 'userGender']=="m" ) echo "checked"; ?> name="userGender" value="m" id="rdMale"/> Male <input type="radio" <?php if($_POST[ 'userGender']=="f" ) echo "checked"; ?> name="userGender" value="f" id="rdFemale"/> Female </td>
                </tr>
                <tr>
                    <td>USER Phone</td>
                    <td><input type="text" maxlength="10" value="<?php echo isset($_POST['userPhone']) ? $_POST['userPhone'] : " "; ?>" name="userPhone" id="userPhone"></td>
                </tr>
                <tr>
                    <td>USER Address</td>
                    <td><textarea name="userAddress" id="userAddress"><?php echo isset($_POST['userAddress']) ? $_POST['userAddress'] : "";?></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>





Example

Also Read :   Dynamic v-model directive | VueJS v-model directive Example

I hope you have Got What is How to Validate Form with PHP – Server Side Validation And how it works.I would Like to have FeadBack From My Blog(live24u.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(live24u.com) Are Most Always Welcome.