Open and test the application 1. Run the application that’s stored in the ex_starts/ch15_ex1 directory. This exercise has modified requirements comparing with the exercise demonstrated during the...

1 answer below »


Open and test the application


1. Run the application that’s stored in the ex_starts/ch15_ex1 directory.
This exercise has modified requirements comparing with the exercise demonstrated during the session!!


2. Test this application by entering valid and invalid data. To test the credit card fields with valid data, you can use a Visa card with a number of 4111111111111111.



Modify the code


3. In the phone() and zip() methods of the Validate class, use the \d pattern instead of the [[:digit:]] pattern. The phone should be displayed in format (999) 999-9999. The zip code should be formatted as a Canadian postal code in the format Z9Z 9Z9 (Z for upper case letter and 9 for digit).


4. In the password() method of the Validate class, modify the code so the password must be at least 12 characters long with at least one uppercase letter, one number, one lower case, and one special character.


5. Modify the index.php file so the Address, City, State, and Zip fields are all required.


6. Add a Birth date field after the Phone number field that requires the user to enter a birth date in this format: mm/dd/yyyy. To get this to work properly, add a birthdate() method to the Validate class. This method should make sure that the birth date isn’t a date in the future.


In the email() method of the Validate class, modify the code so it uses the filter_var() function with the INPUT_VALIDATE_EMAIL filter. (Remember that when you use this function, you don’t include an input type like you do when you use the filter_input() function. This makes the code much shorter.
Answered Same DayAug 06, 2021

Answer To: Open and test the application 1. Run the application that’s stored in the ex_starts/ch15_ex1...

Aditya answered on Aug 10 2021
141 Votes
ch/index.php
require_once('model/fields.php');
require_once('model/validate.php');
$validate = new Validate();
$fields = $validate->getFields();
$fields->addField('email', 'Must be a valid email address.');
$fields->addField('password', 'Must be at least 12 characters.');
$fields->addField('verify');
$fields->addField('first_name');
$fields->addField('last_name');
$fields->addField('address');
$fields->addField('city');
$fields->addField('state', 'Use 2 character abbreviation.');
$fields->addField('zip', 'Use Canadian ZIP code.');
$fields->addField('phone', 'Use 999-999-9999 format.');
$fields->addField('dob', 'Use dd/mm/yyyy format.');
$fields->addField('card_type');
$fie
lds->addField('card_number', 'Enter number with or without dashes.');
$fields->addField('exp_date', 'Use mm/yyyy format.');
$action = filter_input(INPUT_POST, 'action');
if ($action === NULL) {
$action = 'reset';
} else {
$action = strtolower($action);
}
switch ($action) {
case 'reset':
$email = '';
$password = '';
$verify = '';
$firstName = '';
$lastName = '';
$address = '';
$city = '';
$state = '';
$zip = '';
$phone = '';
$cardType = '';
$cardNumber = '';
$cardDigits = '';
$expDate = '';
$dob = '';
include 'view/register.php';
break;
case 'register':
// Copy form values to local variables
$email = trim(filter_input(INPUT_POST, 'email'));
$password = filter_input(INPUT_POST, 'password');
$verify = filter_input(INPUT_POST, 'verify');
$firstName = trim(filter_input(INPUT_POST, 'first_name'));
$lastName = trim(filter_input(INPUT_POST, 'last_name'));
$address = trim(filter_input(INPUT_POST, 'address'));
$city = trim(filter_input(INPUT_POST, 'city'));
$state = filter_input(INPUT_POST, 'state');
$zip = filter_input(INPUT_POST, 'zip');
$phone = filter_input(INPUT_POST, 'phone');
$cardType = filter_input(INPUT_POST, 'card_type');
$cardNumber = filter_input(INPUT_POST, 'card_number');
$cardDigits = preg_replace('/[^[:digit:]]/', '', $cardNumber);
$expDate = filter_input(INPUT_POST, 'exp_date');
$dob = filter_input(INPUT_POST, 'dob');
// Validate form data
$validate->email('email', $email);
$validate->password('password', $password);
$validate->verify('verify', $password, $verify);
$validate->text('first_name', $firstName);
$validate->text('last_name', $lastName);
$validate->text('address', $address);
$validate->text('city', $city);
$validate->state('state', $state);
$validate->zip('zip', $zip);
$validate->phone('phone', $phone);
$validate->cardType('card_type', $cardType);
$validate->cardNumber('card_number', $cardDigits, $cardType);
$validate->expDate('exp_date', $expDate);
$validate->birthdate('dob', $dob);
// Load appropriate view based on hasErrors
if ($fields->hasErrors()) {
include 'view/register.php';
} else {
include 'view/success.php';
}
break;
}
?>
ch/main.css
html {
background-color: rgb(192, 192, 192);
}
body {
font-family: Arial, Helvetica, sans-serif;
width: 900px;
margin: 0 auto;
padding: 0 2em;
background-color: white;
border: 1px solid black;
}
header {
border-bottom: 2px solid black;
padding: .5em 0;
}
header h1 {
color: black;
}
main {
}
aside {
float: left;
width: 150px;
}
section {
float: left;
width: 500px;
}
footer {
clear: both;
border-top: 2px solid black;
}
footer p {
text-align: right;
font-size: 80%;
}
h1 {
font-size: 150%;
margin: 0;
padding: .5em 0 .25em;
}
h2 {
font-size: 120%;
margin: 0;
padding: .75em 0 0;
}
h1, h2 {
color: rgb(208, 133, 4);
}
/* styles for the form */
fieldset {
margin: 1em;
padding-top: 1em;
}
legend {
font-weight: bold;
font-size: 85%;
}
label {
float: left;
width: 10em;
text-align: right;
margin-top: .25em;
margin-bottom: .5em;
}
input, select {
margin-left: 0.5em;
margin-bottom: 0.5em;
width: 14em;
}
br {
clear: both;
}
span {
vertical-align: middle;
}
.error {
color: red;
}
.notice {
color: red;
font-size: 67%;
text-align: right;
}
ch/model/fields.php
class Field {
private $name;
private $message = '';
private $hasError = false;
public function __construct($name, $message = '') {
$this->name = $name;
$this->message = $message;
}
public function getName() { return $this->name; }
public function getMessage() { return $this->message; }
public function hasError() { return $this->hasError; }
public function setErrorMessage($message) {
$this->message = $message;
$this->hasError = true;
}
public function clearErrorMessage() {
$this->message = '';
$this->hasError = false;
}
public function getHTML() {
$message = htmlspecialchars($this->message);
if ($this->hasError()) {
return '' . $message . '';
} else {
return '' . $message . '';
}
}
}
class Fields {
private $fields = array();
public function addField($name, $message = '') {
$field = new Field($name, $message);
$this->fields[$field->getName()] = $field;
}
public function getField($name) {
return $this->fields[$name];
}
public function hasErrors() {
foreach ($this->fields as $field) {
if ($field->hasError()) { return true; }
}
return false;
}
}
?>
ch/model/validate.php
class Validate {
private $fields;
public function __construct() {
$this->fields = new Fields();
}
public function getFields() {
return $this->fields;
}
// Validate a generic text field
public function text($name, $value,
$required = true, $min = 1, $max = 255) {
// Get Field object
$field = $this->fields->getField($name);
// If field is not required and empty, remove errors and exit
if (!$required && empty($value)) {
$field->clearErrorMessage();
return;
}
// Check field and set or clear error message
if ($required && empty($value)) {
$field->setErrorMessage('Required.');
} else if (strlen($value) < $min) {
$field->setErrorMessage('Too short.');
} else if (strlen($value) > $max) {
$field->setErrorMessage('Too long.');
} else {
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here