Skip to content

Instantly share code, notes, and snippets.

@cpjobling
Created May 21, 2012 10:27
Show Gist options
  • Select an option

  • Save cpjobling/2761725 to your computer and use it in GitHub Desktop.

Select an option

Save cpjobling/2761725 to your computer and use it in GitHub Desktop.
An example of server-side validation in PHP
<! --
EG-259 Examinations 2012
Case Study 5
A Registration Form with Server-side Validation using PHP
-->
<?php
# Client-side validation functions
function validateName($name) {
//if it's NOT valid
if(strlen($name) < 4)
return false;
//if it's valid
else
return true;
}
function validateEmail($email) {
return preg_match("/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z]{2,6})$/", $email);
}
function validatePasswords($pass1, $pass2) {
//if DOESN'T MATCH
if(strpos($pass1, ' ') !== false)
return false;
//if are valid
return $pass1 == $pass2 && strlen($pass1) >= 5;
}
function validateMessage($message) {
//if it's NOT valid
if(strlen($message) < 10)
return false;
//if it's valid
else
return true;
}
?>
<?php
# Get form-field names
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
$message = $_POST['message'];
?>
<! DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>EG-259 Examination 2012: Case study 4: Validate Forms using jQuery</title>
<link rel="stylesheet" href="css/general.css" media="screen" />
</head>
<body>
<div id="container">
<h1>Registration process</h1>
<?php if( ([x]($_[y]('[z]')) /* See exam */ &&
(! validateName($name) || ! validateEmail($email) ||
! validatePasswords($password, $password2) ||
! validateMessage($message) ) ):
?>
<div id="error">
<ul>
<?php if(! validateName($name)):
?>
<li>
<strong>Invalid Name:</strong> We want names with more than 3 letters!
</li>
<?php endif ?>
<?php if(! validateEmail($email)):
?>
<li>
<strong>Invalid E-mail:</strong> Stop cowboy! Type a valid e-mail please :P
</li>
<?php endif ?>
<?php if(! validatePasswords($password, $password2)):
?>
<li>
<strong>Passwords are invalid:</strong> Passwords doesn't match or are invalid!
</li>
<?php endif ?>
<?php if(! validateMessage($message)):?>
<li>
<strong>Ivalid message:</strong> Type a message with at least with 10 letters
</li>
<?php endif ?>
</ul>
</div>
<?php elseif ( if ([x]($_[y]('[z]')) /* See exam */ ): ?>
<div id="error" class="valid">
<ul>
<li>
<strong>Congratulations! </strong> All fields are OK ;)
</li>
</ul>
</div>
<?php endif ?>
<form method="post" id="myForm" action="cs5.php">
<div>
<label for="name">Name</label>
<input id="name" name="name" type="text" value="<?= $name ?>" />
<span id="nameInfo">What's your name?</span>
</div>
<div>
<label for="email">E-mail</label>
<input id="email" name="email" type="text" value="<?= $email ?>" />
<span id="emailInfo">Valid E-mail please, you will need it to log in! </span>
</div>
<div>
<label for="password">Password</label>
<input id="password" name="password" type="password" value="<?= $password ?>" />
<span id="passwordInfo">At least 5 characters: letters, numbers and '_'</span>
</div>
<div>
<label for="password2">Confirm Password</label>
<input id="password2" name="password2" type="password" value="<?= $password2 ?>" />
<span id="password2Info">Confirm password</span>
</div>
<div>
<label for="message">Message</label>
<textarea id="message" name="message" value="<?= $message ?>" cols="" rows=""></textarea>
</div>
<div>
<input id="send" name="send" type="submit" value="Send" />
</div>
</form>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="js/validation.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment