srule_
01-07-2008, 01:20 AM
Hello, I'm making a form validation class you may find useful. As of now I only have validation for username, password, and e-mail. Feel free to add to it or make corrections!
<?php
class FormValidation {
//Data Memeber
private $username;
private $email;
private $password;
private $minLength;
private $maxLength;
private $errors = array();
//--------------------------------------------------------------
//CONSTRUCTURE
public function __construct($minLength, $maxLength){
$this->minLength= $minLength;
$this->maxLength = $maxLength;
}
//--------------------------------------------------------------
//Check Username
public function checkUsername($username) {
if ($username == "") {
$this->errors[] = "You didn't enter a username.\n";
}
if (ctype_alpha($username) == FALSE) {
$this->errors[] = "Your username can only consist of letters";
}
if (strlen($username) <= $this->minLength OR strlen($username) >= $this->maxLength) {
$this->errors[] = "The username is the wrong length.\n";
}
if (empty($this->errors)) {
$this->username=$username;
}
}
//--------------------------------------------------------------
//Check E-mail
public function checkEmail($email){
if (empty($email)) {
$this->errors[] = 'You forgot to enter your email.';
}
if ((strpos($email, "@")) === FALSE ||
(strpos($email, ".")) === FALSE ||
(strpos($email, " ")) != FALSE ||
(strpos($email, "@")) === FALSE ||
(strpos($email, "@")) > strrpos($email, "."))
{
$this->errors[] = "Please enter a valid e-mail address";
}
if (empty($this->errors)) {
$this->email=$email;
}
}
//--------------------------------------------------------------
//Check Password
public function checkPassword($password1, $password2){
if (!empty($password1)){
if ($password1 != $password2){
$this->errors[] = 'Your password did not match the confirmed password.';
}
if (strlen($password1) <= $this->minLength OR strlen($password1) >= $this->maxLength){
$this->errors[] = 'Your password must be between 3 and 12 characters';
}
if (ctype_alpha($password) == FALSE) {
$this->result[] = "Your password can only consist of letters";
}
if (empty($this->errors)){
$this->password = $password1;
}
}
else {
$this->errors[] = 'You forgot to enter your password.';
}
}
//--------------------------------------------------------------
// Display Errors
public function displayResults() {
foreach ($this->errors as $msg) { // Print each error.
echo "$msg<br />\n";
}
}
}
?>
<?php
class FormValidation {
//Data Memeber
private $username;
private $email;
private $password;
private $minLength;
private $maxLength;
private $errors = array();
//--------------------------------------------------------------
//CONSTRUCTURE
public function __construct($minLength, $maxLength){
$this->minLength= $minLength;
$this->maxLength = $maxLength;
}
//--------------------------------------------------------------
//Check Username
public function checkUsername($username) {
if ($username == "") {
$this->errors[] = "You didn't enter a username.\n";
}
if (ctype_alpha($username) == FALSE) {
$this->errors[] = "Your username can only consist of letters";
}
if (strlen($username) <= $this->minLength OR strlen($username) >= $this->maxLength) {
$this->errors[] = "The username is the wrong length.\n";
}
if (empty($this->errors)) {
$this->username=$username;
}
}
//--------------------------------------------------------------
//Check E-mail
public function checkEmail($email){
if (empty($email)) {
$this->errors[] = 'You forgot to enter your email.';
}
if ((strpos($email, "@")) === FALSE ||
(strpos($email, ".")) === FALSE ||
(strpos($email, " ")) != FALSE ||
(strpos($email, "@")) === FALSE ||
(strpos($email, "@")) > strrpos($email, "."))
{
$this->errors[] = "Please enter a valid e-mail address";
}
if (empty($this->errors)) {
$this->email=$email;
}
}
//--------------------------------------------------------------
//Check Password
public function checkPassword($password1, $password2){
if (!empty($password1)){
if ($password1 != $password2){
$this->errors[] = 'Your password did not match the confirmed password.';
}
if (strlen($password1) <= $this->minLength OR strlen($password1) >= $this->maxLength){
$this->errors[] = 'Your password must be between 3 and 12 characters';
}
if (ctype_alpha($password) == FALSE) {
$this->result[] = "Your password can only consist of letters";
}
if (empty($this->errors)){
$this->password = $password1;
}
}
else {
$this->errors[] = 'You forgot to enter your password.';
}
}
//--------------------------------------------------------------
// Display Errors
public function displayResults() {
foreach ($this->errors as $msg) { // Print each error.
echo "$msg<br />\n";
}
}
}
?>