Recap: Input Validation
Part of the Logic & Flow section of Coddy's PHP journey — lesson 58 of 68.
Challenge
EasyYou will receive four inputs: an email address string, a phone number string, a minimum age number, and an actual age number.
Create two custom exception classes:
InvalidEmailExceptionthat extends the baseExceptionclassInvalidAgeExceptionthat extends the baseExceptionclass
Create a function called validateUserData that accepts four parameters: email, phone, minimum age, and actual age. Inside this function:
- Check if the email contains the
@symbol. If it doesn't, throw anInvalidEmailExceptionwith the message"Email must contain @ symbol" - Check if the phone number length is exactly 10 characters. If it's not, throw a generic
Exceptionwith the message"Phone number must be 10 digits" - Check if the actual age is less than the minimum age. If it is, throw an
InvalidAgeExceptionwith the message"User must be at least X years old"(where X is the minimum age value) - If all validations pass, print
"User data is valid"
Use a try...catch...finally block to call the function with the provided inputs. Create two separate catch blocks:
- The first
catchblock should catchInvalidEmailExceptionand print"Email error: "followed by the exception message - The second
catchblock should catchInvalidAgeExceptionand print"Age error: "followed by the exception message - Add a third
catchblock that catches any otherExceptionand prints"Validation error: "followed by the exception message
In the finally block, print "Validation process completed".
Input format:
- First line: A string representing the email address (example:
user@example.comorinvalidemail.com) - Second line: A string representing the phone number (example:
1234567890or12345) - Third line: An integer representing the minimum age requirement (example:
18) - Fourth line: An integer representing the actual age (example:
25or16)
Expected output:
- If all data is valid:
User data is validfollowed byValidation process completedon a new line - If email is invalid:
Email error: Email must contain @ symbolfollowed byValidation process completedon a new line - If phone number is invalid:
Validation error: Phone number must be 10 digitsfollowed byValidation process completedon a new line - If age is below minimum:
Age error: User must be at least X years old(where X is the minimum age) followed byValidation process completedon a new line
Try it yourself
<?php
// Read inputs
$email = trim(fgets(STDIN));
$phone = trim(fgets(STDIN));
$minAge = intval(fgets(STDIN));
$actualAge = intval(fgets(STDIN));
// TODO: Create custom exception classes here
// Create InvalidEmailException class
// Create InvalidAgeException class
// TODO: Create validateUserData function here
// The function should accept four parameters: email, phone, minAge, actualAge
// Implement validation logic inside the function
// TODO: Use try...catch...finally block to call the function
// Call validateUserData with the input values
// Add catch blocks for InvalidEmailException, InvalidAgeException, and generic Exception
// Add finally block
?>All lessons in Logic & Flow
1Advanced Functions
Anonymous FunctionsClosures and 'use'Arrow FunctionsCallback FunctionsUsing 'call_user_func'Variable FunctionsPassing by ReferenceRecursive FunctionsRecap: Function Medley4Multi-dimensional Arrays
Creating a 2D ArrayAccessing 2D Array ElementsModifying 2D Array ElementsIterating with Nested Loops2D Associative ArraysRecap: Simple Grid Exercise2Advanced Array Manipulations
Adding with 'array_push'Removing with 'array_pop'Adding with 'array_unshift'Removing with 'array_shift'Merging Indexed ArraysMerging Associative ArraysExtracting with 'array_slice'Values with 'in_array'Keys with 'array_search'Recap: Playlist Exercise5Student Gradebook
Project Setup: Data StructureAdding a New StudentAdding a Grade to a StudentCalculating Student's AverageFinding the Top StudentGenerating a Report Card8Error and Exception Handling
Understanding PHP ErrorsThe 'try...catch' BlockThe 'finally' BlockThrowing an ExceptionCreating a Custom ExceptionUsing a Custom ExceptionRecap: Input Validation3Sorting Arrays
Sort Indexed Arrays AscendingSort Indexed Arrays DescendingSort Assoc Arrays by ValueSort Assoc Arrays by KeyNatural Order SortingCustom Sorting with 'usort'Recap: Leaderboard Sorting