...

View Full Version : Form Validation Class



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";
}
}


}
?>

aedrin
01-11-2008, 06:05 PM
This looks like procedural code that got converted to OO code.

It's not the proper way to use this, as it's not reusable at all.

Not to mention that you should probably use regular expressions for some of these (such as email).

Brandoe85
01-11-2008, 07:11 PM
This looks like procedural code that got converted to OO code.

It's not the proper way to use this, as it's not reusable at all.

Not to mention that you should probably use regular expressions for some of these (such as email).

If it's procedural and then was converted to OO...then how can it still be procedural? ;)

whatever you were saying, I agree. The use of the member variables and the parameters you are taking in for the functions seem confusing. Also using echo's in classes and functions kind of defeats the point.

mountainpride
02-21-2008, 09:29 PM
How do I have PHP check the database it is posting to, and
tell the user if the username is unavailable.

Fou-Lu
05-31-2008, 07:05 PM
You cannot.
I'm guessing that srule is new to oop. And thats ok. Your syntax is correct, its just the concept is a little... off. Think more abstractly at the validation process - I think it was aedrin that mentioned you should use regular expressions, which I would agree with, map some regexp to class constants (no enum unfortunately :(). Consider something that takes any input from any field, and is told by you how to validate it, so it can be called more evenly, with a prototype of validate($data, $type): Validator::validate($username, Validator::USERNAME), Validator::validate($gender, Validator::RADIO) kind of an idea.
Control error reporting by throwing exceptions from your classes and capturing them in your main.

Good start though, think a little more abstractly (consider using static methods). I'm interested to see what this will come out with.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum