Custom PHP Validation Logic
A custom PHP validation logic to validate age in PHP.unknown
php
2 years ago
869 B
3
Indexable
<?php function validateAge($age) { // Check if the age is not empty if (empty($age)) { return "Age is required."; } // Check if the age is a positive integer if (!ctype_digit($age) || $age <= 0) { return "Age must be a positive integer."; } // Check if the age is within a specific range if ($age < 18 || $age > 99) { return "Age must be between 18 and 99."; } // If all validation passes, return true return true; } // Example usage $age = $_POST['age']; // Validate the age $validationResult = validateAge($age); // Check the validation result if ($validationResult === true) { // Age is valid, proceed with further actions echo "Age is valid."; } else { // Age is invalid, display error message echo "Invalid age: " . $validationResult; } ?>
Editor is loading...