w3resource

PHP validation class: Email, password, and field validation


19. Validation Class with Static Methods

Write a class called 'Validation' with static methods to validate email addresses, passwords, and other common input fields.

Sample Solution:

PHP Code :

<?php
class Validation {
    public static function validateEmail($email) {
        // Check if the email is valid using a regular expression
        return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
    }

    public static function validatePassword($password) {
        // Here's an example that checks if the password is at least 8 characters long
        return strlen($password) >= 8;
    }

    public static function validateField($field) {
        // Here's an example that checks if the field is not empty
        return !empty($field);
    }
}

$email = "[email protected]";
$password = "password123";
$field = "";

if (Validation::validateEmail($email)) {
    echo "Email is valid.</br>";
} else {
    echo "Email is invalid.</br>";
}

if (Validation::validatePassword($password)) {
    echo "Password is valid.</br>";
} else {
    echo "Password is invalid.v";
}

if (Validation::validateField($field)) {
    echo "Field is valid.</br>";
} else {
    echo "Field is invalid.</br>";
}
?>

Sample Output:

Email is valid.
Password is valid.
Field is invalid.

Explanation:

In the above exercise -

  • The "Validation" class has three static methods: validateEmail(), validatePassword(), and validateField(), which validate email addresses, passwords, and other common input fields, respectively.
  • The "validateEmail($email)" method uses the FILTER_VALIDATE_EMAIL filter to check if the email is valid according to the provided regular expression. It returns true if the email is valid, and false otherwise.
  • The "validatePassword($password)" method checks if the password meets your specified requirements. In this example, it checks if the password is at least 8 characters long. It returns true if the password is valid, and false otherwise.
  • The "validateField($field)" method validates other common input fields. In this example, it checks if the field is not empty. It returns true if the field is valid, and false otherwise.

Flowchart:

Flowchart: Singleton design pattern implementation.

For more Practice: Solve these Related Problems:

  • Write a PHP class Validation with static methods to validate email addresses and passwords, and then test these methods with various input cases.
  • Write a PHP script to implement a static method for validating phone numbers and another for checking URL formats in a Validation class.
  • Write a PHP function to validate multiple input fields using static methods and return a summary of validation statuses.
  • Write a PHP program to extend the Validation class with additional static methods for common data formats and test them against a sample dataset.

Go to:


PREV : Logger with Singleton Design Pattern.
NEXT : PHP arrays Exercises Home.

PHP Code Editor:



Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.