PHP Form Validation With Regular Expressions Example

PHP 8 , PHP

Nicesnippets

716

18-06-2021


PHP Form Validation With Regular Expressions Example

Hi guys,

Today i will explained how to use php form validation with Regular Expressions in your form. This example is so easy to use in php. This example to i am a create a bootstrap form and set to the required validation & Regular Expressions validation in your form input.

This example to i am create a name,email,website,comment,and gender field and set to the required validation & Regular Expressions validation in php.

So let's start to the example.

Example :


index.php

<?php

$nameErr = $emailErr = $genderErr = $websiteErr = "";

$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

if (empty($_POST["name"])) {

$nameErr = "Name is required";

} else {

$name = test_input($_POST["name"]);

// check if name only contains letters and whitespace

if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) {

$nameErr = "Only letters and white space allowed";

}

}

if (empty($_POST["email"])) {

$emailErr = "Email is required";

} else {

$email = test_input($_POST["email"]);

// check if e-mail address is well-formed

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

$emailErr = "Invalid email format";

}

}

if (empty($_POST["website"])) {

$websiteErr = "website is required";

} else {

$website = test_input($_POST["website"]);

// check if URL address syntax is valid

if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[[email protected]#\/%?=~_|!:,.;]*[[email protected]#\/%=~_|]/i",$website)) {

$websiteErr = "Invalid URL";

}

}

if (empty($_POST["comment"])) {

$commentErr = "Comment is required";

} else {

$comment = test_input($_POST["comment"]);

}

if (empty($_POST["gender"])) {

$genderErr = "Gender is required";

} else {

$gender = test_input($_POST["gender"]);

}

}

function test_input($data) {

$data = trim($data);

$data = stripslashes($data);

$data = htmlspecialchars($data);

return $data;

}

?>

<!DOCTYPE html>

<html lang="en">

<head>

<title>PHP Form Validation With Regular Expressions Example</title>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

<style>

.error {

color: #FF0000;

}

</style>

</head>

<body>

<div class="container">

<h2>PHP Form Validation With Regular Expressions Example</h2>

<p><span class="error">* All Fields required</span></p>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

<div class="form-group">

<label for="name">Name:<span class="error">*</span></label>

<input type="text" class="form-control" id="name" placeholder="Enter Name" name="name">

<span class="error"><?php echo $nameErr;?></span>

</div>

<div class="form-group">

<label for="email">Email:<span class="error">*</span></label>

<input type="email" class="form-control" id="email" placeholder="Enter email" name="email">

<span class="error"><?php echo $emailErr;?></span>

</div>

<div class="form-group">

<label for="website">Website:</label>

<input type="website" class="form-control" id="website" placeholder="Enter Website" name="website">

<span class="error"><?php echo $websiteErr;?></span>

</div>

<div class="form-group">

<label>Comment:</label>

<textarea name="comment" class="form-control" rows="5" cols="40"></textarea>

<span class="error"><?php echo $commentErr;?></span>

</div>

<label>Gender:<span class="error">*</span></label><br>

<label class="radio-inline">

<input type="radio" name="gender" value="male"> Male

</label>

<label class="radio-inline">

<input type="radio" name="gender" value="female"> Female

</label>

<label class="radio-inline">

<input type="radio" name="gender" value="other"> Other

</label><br>

<span class="error"><?php echo $genderErr;?></span>

<br>

<button type="submit" class="btn btn-primary">Submit</button>

</form>

</div>

<div class="container mt-3 mb-3">

<div class="row">

<div class="col-md-12">

<?php

echo "<h2>Your Output:</h2>";

echo $name;

echo "<br>";

echo $email;

echo "<br>";

echo $website;

echo "<br>";

echo $comment;

echo "<br>";

echo $gender;

?>

</div>

</div>

</div>

</body>

</html>

Output

Your Output:

Evelyn Mason

[email protected]

https://tryphp.w3schools.com/

how are you.

male

now you can check your own.

i hope it can help you...


Recommended Posts