Complete the code to check if the input is a valid email.
<?php $email = $_POST['email']; if (filter_var($email, [1])) { echo "Valid email."; } else { echo "Invalid email."; } ?>
Use FILTER_VALIDATE_EMAIL to check if the input is a valid email format.
Complete the code to sanitize the input by removing illegal characters from an email.
<?php $email = $_POST['email']; $clean_email = filter_var($email, [1]); echo $clean_email; ?>
Use FILTER_SANITIZE_EMAIL to remove illegal characters from the email input.
Fix the error in the code to properly validate an integer input.
<?php $age = $_POST['age']; if (filter_var($age, [1])) { echo "Valid age."; } else { echo "Invalid age."; } ?>
Use FILTER_VALIDATE_INT to check if the input is a valid integer.
Fill both blanks to create a sanitized and validated username input.
<?php $username = $_POST['username']; $clean_username = filter_var($username, [1]); if (preg_match([2], $clean_username)) { echo "Valid username."; } else { echo "Invalid username."; } ?>
First sanitize the username to remove unwanted characters using FILTER_SANITIZE_STRING. Then validate it with a regular expression that allows letters, numbers, and underscores between 3 and 20 characters.
Fill all three blanks to sanitize and validate a URL input.
<?php $url = $_POST['url']; $clean_url = filter_var($url, [1]); if (filter_var($clean_url, [2]) && preg_match([3], $clean_url)) { echo "Valid URL."; } else { echo "Invalid URL."; } ?>
Sanitize the URL first with FILTER_SANITIZE_URL. Then validate it with FILTER_VALIDATE_URL. Finally, use a regex to ensure it starts with http or https.