Validation
The Validation
class provides methods to validate user input and protect against vulnerabilities like SQL injection and cross-site scripting.
Available Methods:
1. validateEmail(email)
:
- Purpose: Validate email addresses.
- Returns:
true
if the email is valid,false
otherwise. - Example:
$email = "[email protected]";
if (Validation::validateEmail($email)) {
echo "Email is valid.";
} else {
echo "Email is invalid.";
}
- Source: src/Validation.php (Line 13-19)
2. validatePassword(password)
:
- Purpose: Validate passwords for strength, enforcing minimum length and character complexity.
- Returns:
true
if the password is valid,false
otherwise. - Example:
$password = "P@$$wOrd";
if (Validation::validatePassword($password)) {
echo "Password is valid.";
} else {
echo "Password is invalid.";
}
- Source: src/Validation.php (Line 21-27)
3. validateUsername(username)
:
- Purpose: Validate usernames, ensuring they adhere to specific format and length requirements.
- Returns:
true
if the username is valid,false
otherwise. - Example:
$username = "johndoe123";
if (Validation::validateUsername($username)) {
echo "Username is valid.";
} else {
echo "Username is invalid.";
}
- Source: src/Validation.php (Line 29-35)
4. validateText(text, maxLength = 255)
:
- Purpose: Validate text input, limiting its length and preventing potential XSS attacks.
- Returns:
true
if the text is valid,false
otherwise. - Example:
$text = "This is a test text.";
if (Validation::validateText($text)) {
echo "Text is valid.";
} else {
echo "Text is invalid.";
}
- Source: src/Validation.php (Line 37-43)
5. validateNumber(number)
:
- Purpose: Validate numeric input, ensuring it’s a valid number.
- Returns:
true
if the number is valid,false
otherwise. - Example:
$number = 12345;
if (Validation::validateNumber($number)) {
echo "Number is valid.";
} else {
echo "Number is invalid.";
}
- Source: src/Validation.php (Line 45-51)
6. validateUrl(url)
:
- Purpose: Validate URLs, ensuring they adhere to a valid format.
- Returns:
true
if the URL is valid,false
otherwise. - Example:
$url = "https://www.example.com";
if (Validation::validateUrl($url)) {
echo "URL is valid.";
} else {
echo "URL is invalid.";
}
- Source: src/Validation.php (Line 53-59)
7. validateDate(date, format = "Y-m-d")
:
- Purpose: Validate dates using a specified format.
- Returns:
true
if the date is valid,false
otherwise. - Example:
$date = "2023-12-25";
if (Validation::validateDate($date)) {
echo "Date is valid.";
} else {
echo "Date is invalid.";
}
- Source: src/Validation.php (Line 61-67)
8. validatePhone(phone)
:
- Purpose: Validate phone numbers based on a specific format.
- Returns:
true
if the phone number is valid,false
otherwise. - Example:
$phone = "+1 (555) 555-5555";
if (Validation::validatePhone($phone)) {
echo "Phone number is valid.";
} else {
echo "Phone number is invalid.";
}
- Source: src/Validation.php (Line 69-75)
This is just a brief outline of the available validation methods within the Validation
class. You can find further details and customization options by referring to the source code.