Validations in PHP

Validations
in PHP

Validation is used to check whether user has submitted
the input or not.There are two
types of validation:

·Client
side validation: Validation is performed on the client
machine web browser.

·Server
Side validation: After submitted by user,The data has sent to
a server and perform validation checks in server machine.

Client
Side validation:

Client side validation is done by usingJavascript. Javascript validation is one of the common features used by
language. Interactive websites contains dynamic content that requires user
input and that input must be valid.

Code Implementation:

Step1: First
we will create an HTML Form . Let the name of the page form.php:

//If any of the fields is left empty or null,this function
will validate the fields and alerts a
message to returns false,to prevent the form being submitted

var
uname=document.forms["myForm"]["uname"].value;

var
uname=document.forms["myForm"]["pass"].value;

var
uname=document.forms["myForm"]["name"].value;

var
uname=document.forms["myForm"]["phone"].value;

if(uname == null || uname == "" )

{

alert("Username
must be filled.");

return false;

}

if (document.getElementById('year').value
== 'Select')

{

alert("Please select Year");

return false;

}

else if
(document.getElementById('month').value == 'Select')

{

alert("Please select Month");

return false;

}

else if
(document.getElementById('day').value == 'Select')

{

alert("Please select
Day");

return false;

}

}

</script>

Code
Output:

Now, lets run our sample, and check whether it validate
form data or not. Left all the fields blank with null values and try to submit,
the form will immediately popup a message “UserName
must be filled”.

Similarly, left the other fields blank and check whether
it validate form and show appropriate message or not.

Server
Side validation:

In
this post you’ll learn Server side validation using PHP.
Validation using PHP are considered to be more secure .PHP has a wide variety
of functions and features which is used to validate a form.

<?php

//when the form is submmited it will insert the values entered
by user

else
if(empty($month)) //if month is empty than
message would be given

{

$msg=
"Please enter Month";

}

else
if(empty($day)) //if Day is empty than
message would be given

{

$msg= "Please enter Day";

}

else
if(empty($phone)) //if phone is empty than
message would be given

{

?>

<strong style=background-color:red>Please
enter Phone</strong>

<?php

}

}

}

?>

//Style is used in the form

<style>

input[type=text],input[type=number],input[type=password]

{

padding: 10px 20px;

margin: 3px 0;

display: inline-block;

border: 1px solid #ccc;

border-radius: 6px;

box-sizing: border-box;

marg

}

.user

{

background-color:lightgrey;

width:40%;

margin-top:20px;

}

</style>

Code
Output:

Now, lets run our sample, and check whether it validate
form data or not. Left all the fields blank with null values and try to submit,
the form will immediately popup a message “Please
enter username”.

Similarly left the other fields blank and check whether
it validate form and show appropriate message or not.

Validation
using Regular Expression (Server side):

We will show you how to validate names, e-mails and URL’s:

PHP-Validate e-mails:

The code below show the validation in e-mail :

Step1:
Create an HTML page email.php

<style>

.text

{

text-align:center;

line-height:525px;

</style>

<form
method="post" class="text">

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

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

</form>

<?php

requie_once(“validate.php”);

?>

Step2:Create
validate.php:

<?php

//when the form is submmited it will
insert the values entered by user

if(isset($_POST["sub"]))

{

$email=$_POST["email"];

//the pattern is "any letter or
number followed by @ followed by any letter or number

Code
Output:

Incomplete information

Some other methods are missing, for example password validation could also be done by using charCodeAt() method in order to check whether the particular character is entered or not we know the ascii value of A-Z ranges from 65 to 90 so if user will not enter any capital letter then it will show the validation message similarly other character will be verified and we can also use required attribute in the input tag for simple validation.