use Respect\Validation\Validator as v; $email = "example@example.com"; try { if (!v::email()->validate($email)) { throw new Exception("Invalid email address."); } else { // Proceed with the validation logic } } catch (Exception $e) { echo $e->getMessage(); }
use Respect\Validation\Validator as v; $phone = "+1 234-567-890"; try { if (!v::phone()->validate($phone)) { throw new Exception("Invalid phone number."); } else { // Proceed with the validation logic } } catch (Exception $e) { echo $e->getMessage(); }In these examples, we have used the "Respect/Validation" library to validate an email address and a phone number. If the validation fails, we throw an exception with a custom error message that can be caught and displayed to the user.