Refactored the validate function in apply.php