Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

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 XXXXXXXXXX.

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 XXXXXXXXXXThe 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 Day Aug 06, 2021

Solution

Aditya answered on Aug 10 2021
155 Votes
ch/index.php
?php
equire_once('model/fields.php');
equire_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 a
eviation.');
$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');
$fields->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
egister.php';

eak;
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-
irthdate('dob', $dob);

Load appropriate view based on hasE
ors
if ($fields->hasE
ors()) {
include 'view
egister.php';
} else {
include 'view/success.php';
}

eak;
}
?
ch/main.css
html {
background-color: rgb(192, 192, 192);
}
ody {
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;
}
{
clear: both;
}
span {
vertical-align: middle;
}
.e
or {
color: red;
}
.notice {
color: red;
font-size: 67%;
text-align: right;
}
ch/model/fields.php
?php
class Field {
private $name;
private $message = '';
private $hasE
or = 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 hasE
or() { return $this->hasE
or; }
public function setE
orMessage($message) {
$this->message = $message;
$this->hasE
or = true;
}
public function clearE
orMessage() {
$this->message = '';
$this->hasE
or = false;
}
public function getHTML() {
$message = htmlspecialchars($this->message);
if ($this->hasE
or()) {
return '' . $message . '
span>';
} else {
return '' . $message . '
span>';
}
}
}
class Fields {
private $fields = a
ay();
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 hasE
ors() {
foreach ($this->fields as $field) {
if ($field->hasE
or()) { return true; }
}
return false;
}
}
?
ch/model/validate.php
?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 e
ors and exit
if (!$required && empty($value)) {
$field->clearE
orMessage();
return;
}

Check field and set or clear e
or message
if ($required && empty($value)) {
$field->setE
orMessage('Required.');
} else if (strlen($value) < $min) {
$field->setE
orMessage('Too short.');
} else if (strlen($value) > $max) {
$field->setE
orMessage('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