0% found this document useful (0 votes)
16 views6 pages

Regular Expression

Uploaded by

23pca005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

Regular Expression

Uploaded by

23pca005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

19.Create a PHP Program using regular expressions.

AIM:
The aim of the Login Form Program is to authenticate a user's credentials by validating
their name, email, phone number, and password according to predefined rules.

Algorithm:

STEP1: Load form with input fields for name, email, phone number, and password.

STEP 2: Style the form for a user-friendly experience.

STEP 3: Submit form data via POST request to the same page.

STEP 4: Trigger server-side validation on form submission.

STEP 5: Validation Logic:

o Check if name field is filled and contains only letters and spaces.
o Check if email field is filled and validate format using filter_var().
o Check if phone number field is filled and contains exactly 10 digits.
o Check if password field is filled and meets security criteria (8-20 characters,
lowercase, uppercase, number).

STEP 6: If all fields are valid, display "LOGGED IN" message.

STEP 7: If any field is invalid, display respective error messages.

PROGRAM CODING:

login.php:

<!DOCTYPE html>
<html>
<head>
<title>LOGIN</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></
script>
<style>
div#header {
font-family: Vivaldi;
text-align: center;
}

#header h1 {
font-family: Vivaldi;
color: greenyellow;
font-size: 47px;
}

#loginform {
text-align: left;
padding: 10px;
max-width: 500px;
font-family: JosefinSans;
font-size: 20px;
}

.form-control {
margin: 15px;
padding: 10px;
color: black;
border: 2px solid yellowgreen;
border-radius: 10px;
background-color: lightyellow;
font-family: JosefinSans;
font-size: 15px;
}

.container {
max-width: 600px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.error {
color: red;
}

.success {
color: green;
}
</style>
</head>
<body>
<?php
$nameErr = $emailErr = $psErr = $phoneErr = "";
$name = $email = $pass = $phone = "";
$successMsg = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$isValid = true;

if (empty($_POST["name"])) {
$nameErr = "Name is required";
$isValid = false;
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z-' ]*$/", $name)) {
$nameErr = "Only letters and white space allowed";
$isValid = false;
}
}

if (empty($_POST["email"])) {
$emailErr = "Email is required";
$isValid = false;
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
$isValid = false;
}
}

if (empty($_POST["phone"])) {
$phoneErr = "Phone number is required";
$isValid = false;
} else {
$phone = test_input($_POST["phone"]);
if (!preg_match("/^[0-9]{10}$/", $phone)) {
$phoneErr = "Invalid phone number, must be 10 digits";
$isValid = false;
}
}

if (empty($_POST["pswrd"])) {
$psErr = "Password is required";
$isValid = false;
} else {
$pass = test_input($_POST["pswrd"]);
if (!preg_match("#.*^(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$#", $pass)) {
$psErr = "Letters (a-z, A-Z) and Numbers (0-9) must be in your password";
$isValid = false;
}
}

if ($isValid) {
$successMsg = "Registered successfully!";
}
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

<div id="header">
<h1 id="title">Login</h1>
</div>
<center>
<div class="container">
<form id="loginform" method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<br>
<div class="form-group">
<br>
<label id="name-label" for="name">Name: </label><br>
<input type="text" name="name" id="name" class="form-control"
placeholder="Please Enter Your Name" value="<?php echo $name; ?>">
<span class="error"><?php echo $nameErr;?></span>
</div>
<br>
<div class="form-group">
<label id="email-label" for="email">Email ID: </label><br>
<input type="email" name="email" id="email" class="form-control"
placeholder="Please Enter Your Valid Email ID" value="<?php echo $email; ?>">
<span class="error"><?php echo $emailErr;?></span>
</div>
<br>
<div class="form-group">
<label id="phone-label" for="phone">Phone Number: </label><br>
<input type="text" name="phone" id="phone" class="form-control"
placeholder="Please Enter Your Phone Number" value="<?php echo $phone; ?>">
<span class="error"><?php echo $phoneErr;?></span>
</div>
<br>
<div class="form-group">
<label id="password-label" for="password">Password: </label><br>
<input type="password" name="pswrd" id="password" class="form-control"
placeholder="Please Enter Your Password">
<span class="error"><?php echo $psErr;?></span>
</div>
<br>
<div class="form-group">
<center><button type="submit" id="submit" class="btn
btn-success">Submit</button></center>
</div>

<?php if ($successMsg): ?>


<p class="success"><?php echo $successMsg; ?></p>
<?php endif; ?>
</form>
</div>
</center>
</body>
</html>

Success.php

<!DOCTYPE html>
<html>
<head>
<title>Registration Successful</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1 class="text-center">Registration Successful!</h1>
<p class="text-center">Thank you for registering. You can now log in with your
credentials.</p>
<div class="text-center">
<a href="login.php" class="btn btn-primary">Back to Login</a>
</div>
</div>
</body>
</html>
OUTPUT:

You might also like