Menu
Coddy logo textTech

Recap: Input Validation

Part of the Logic & Flow section of Coddy's PHP journey — lesson 58 of 68.

challenge icon

Challenge

Easy

You 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:

  • InvalidEmailException that extends the base Exception class
  • InvalidAgeException that extends the base Exception class

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 an InvalidEmailException with the message "Email must contain @ symbol"
  • Check if the phone number length is exactly 10 characters. If it's not, throw a generic Exception with the message "Phone number must be 10 digits"
  • Check if the actual age is less than the minimum age. If it is, throw an InvalidAgeException with 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 catch block should catch InvalidEmailException and print "Email error: " followed by the exception message
  • The second catch block should catch InvalidAgeException and print "Age error: " followed by the exception message
  • Add a third catch block that catches any other Exception and 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.com or invalidemail.com)
  • Second line: A string representing the phone number (example: 1234567890 or 12345)
  • Third line: An integer representing the minimum age requirement (example: 18)
  • Fourth line: An integer representing the actual age (example: 25 or 16)

Expected output:

  • If all data is valid: User data is valid followed by Validation process completed on a new line
  • If email is invalid: Email error: Email must contain @ symbol followed by Validation process completed on a new line
  • If phone number is invalid: Validation error: Phone number must be 10 digits followed by Validation process completed on 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 by Validation process completed on 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