0% found this document useful (0 votes)
2 views

16.1 . PHP Practical - Validate Input Data Using Regular Expression

The document provides an overview of form validation in PHP, detailing the importance of validating user input in HTML forms. It includes code examples for validating empty strings, strings, numbers, email addresses, and URLs using regular expressions. Each validation method is accompanied by PHP code snippets that demonstrate how to implement these checks effectively.

Uploaded by

agauubaal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

16.1 . PHP Practical - Validate Input Data Using Regular Expression

The document provides an overview of form validation in PHP, detailing the importance of validating user input in HTML forms. It includes code examples for validating empty strings, strings, numbers, email addresses, and URLs using regular expressions. Each validation method is accompanied by PHP code snippets that demonstrate how to implement these checks effectively.

Uploaded by

agauubaal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

CCCS - 415

Advanced Web Development Using


Form Validation in PHP
• An HTML form contains various input
fields such as text box, checkbox, radio
buttons, submit button, and checklist,
etc. These input fields need to be
validated, which ensures that the user
has entered information in all the
required fields and also validates that the
information provided by the user is valid
and correct.
• There is no guarantee that the
information provided by the user is
always correct. PHP validates the data at
You need to
validate a few
things:
1.Empty String
2.Validate String
3.Validate Numbers
4.Validate Email
5.Validate URL
Empty String
The code below checks that
the field is not empty. If the
user leaves the required
field empty, it will show an
error message. Put these
lines of code to validate the
required field.
<?php
if (empty($_POST["name"]) &&
empty($_POST["email"] )) {
$errMsg = "Error! You didn't enter the Name or
Email";
echo $errMsg;
} else {
$name = $_POST["name"];
$email = $_POST["email"];
echo "Welcome, ".$_POST["name"];
echo "<br/> Your email address is: ".
$_POST["email"];
}
?>
Pattern
Matching
The preg_match() function
returns whether a match was found
in a string.
Syntax:
preg_match(pattern,input
)
Parameter Values
Parameter Description
Required. Contains a regular
pattern expression indicating what to
search for
Required. The string in which the
input search will be performed
Simple regex chars
[abc] A single character: a, b or c
[^abc] Any single character but a, b, or c
[a-z] Any single character in the range a-z
[a-zA-Z] Any single character in the range a-z or A-Z
^ Start of line
$ End of line
\A Start of string
\z End of string
. Any single character
\s Any whitespace character
\S Any non-whitespace character
\d Any digit
\D Any non-digit
\w Any word character (letter, number, underscore)
\W Any non-word character
\b Any word boundary character
(...) Capture everything enclosed
(a|b) a or b
a? Zero or one of a
a* Zero or more of a
a+ One or more of a
a{3} Exactly 3 of a
a{3,} 3 or more of a
a{3,6} Between 3 and 6 of a
Validate String
The code below checks that
the field will contain only
alphabets, for example -
name. If the name field does
not receive valid input from
the user, then it will show
an error message:
<?php
$name = $_POST ["name"];
if (!preg_match ("/^[a-zA-z]", $name) ) {
$ErrMsg = "Only alphabets are allowed.";
echo $ErrMsg;
} else {
echo $name;
}

?>
Validate Number
The below code validates
that the field will only
contain a numeric
value. For example
- Mobile no. If the Mobile
no field does not receive
numeric data from the user,
<?php
if (!preg_match ("/^[0-9]{10}$/", $mobileno) ){
$ErrMsg = "Only 10 numeric values allowed.";
{
$ErrMsg = "Only numeric value is allowed.";
echo $ErrMsg;
}
else
{
echo $mobileno;
}
?>
Validate Email
• A valid email must contain @ and .
symbols. PHP provides various
methods to validate the email
address. Here, we will use regular
expressions to validate the email
address.

• The below code validates the email


address provided by the user
through HTML form. If the field does
not contain a valid email address,
<?php
$email = $_POST ["Email"];
$pattern = "[a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]
+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix";
if (!preg_match ($pattern, $email) ){
$ErrMsg = "Email is not valid.";
echo $ErrMsg;
} else {
echo "Your valid email address is: " .$email;
}
?>
Validate URL
The below code validates the
URL of website provided by
the user via HTML form. If the
field does not contain a valid
URL, the code will display an
error message, i.e., "URL is
not valid".
$website = $_POST["website"];
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-
z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",
$website))
{
$websiteErr = "URL is not valid";
echo $websiteErr;
} else
{
echo "Website URL is: " .$website;
}
?>

You might also like