A (static) PHP validator class. Lets you validate data according to different sets of rules, listed below.
minLength($string, $max) Checks if a string has a minimum of x characters.
<?php
$name = "Hanna";
if (Validator::isMinLength($name, 3)) echo "Valid";
?>maxLength($string, $max)
Checks if a string has a maximum of x characters.
<?php
$name = "Hanna";
if (!Validator::isMaxLength($name, 3)) echo "Name is too long (maximum 3 characters), use a shorter name";
?>matches($string, $matches)
Checks if a string matches another.
<?php
$name = "Hanna";
$matches = "Hanna";
if (Validator::matches($name, $matches)) {
echo "Name matches";
} else {
echo "Name is not valid";
}
?>hasNoSpecialChars($string)
Checks that a string has no special characters: ['^£$%&*}{@#~><>|=_+¬]
<?php
$string = "<script>window.alert("Hello");</script>";
if (Validator::hasNoSpecialChars($string)) {
echo "Contains invalid characters!";
}
?>timeStamp($string)
Checks that a string is a valid timestamp.
<?php
$timestamp = "2016-02-15";
if (Validator::isTimeStamp($timestamp)) {
echo "This is a valid timestamp.";
}
?>yearMonth($string)
Checks that a string is made of year and month.
<?php
$yearMonth = "2016-02";
if (Validator::isYearMonth($yearMonth)) {
echo "This is a valid.";
}
?>alphabetic($string)
Checks that a string is alphabetic.
<?php
$string = "2016-02";
if (!Validator::isAlphabetic($string)) {
echo "Must contain _letters_ only."
}
?>alphaNumeric($string)
Checks that a string is alphanumeric.
<?php
$string = "Month12";
if (Validator::isAlphaNumeric($string)) {
echo "This is a valid string.";
}
?>digit($string)
Checks that a string has only digits.
<?php
$string = "12345";
if (Validator::isDigit($string)) {
echo "This is a valid string.";
}
?>email($string)
Checks that a string is an email.
<?php
$string = "info@hannasoderstrom.com";
if (Validator::isEmail($string)) {
echo "This is a valid email.";
}
?>URL($string)
Checks that a string is an URL.
<?php
$string = "http://www.hannasoderstrom.com";
if (Validator::isUrl($string)) {
echo "This is a valid URL.";
}
?>name($string)
Checks that a string is a name (only space and letters)
<?php
$string = "Hanna Söderström";
if (Validator::isName($string)) {
echo "This is a valid name.";
}
?>html($string)
Escapes a string so that it can safely be used in HTML.
<p>Name: <?php Validator::html($name); ?></p>- Hanna Söderström
- info@hannasoderstrom.com