Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-07-2008, 01:20 AM   PM User | #1
srule_
Regular Coder

 
Join Date: Jul 2007
Posts: 571
Thanks: 25
Thanked 28 Times in 28 Posts
srule_ is an unknown quantity at this point
Form Validation Class

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 Code:
<?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";
            }
    }


}
?>

Last edited by srule_; 01-07-2008 at 02:58 AM..
srule_ is offline   Reply With Quote
Old 01-11-2008, 06:05 PM   PM User | #2
aedrin
Senior Coder

 
Join Date: Jan 2007
Posts: 1,648
Thanks: 1
Thanked 58 Times in 54 Posts
aedrin will become famous soon enough
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).
aedrin is offline   Reply With Quote
Old 01-11-2008, 07:11 PM   PM User | #3
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
Quote:
Originally Posted by aedrin View Post
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.
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 02-21-2008, 09:29 PM   PM User | #4
mountainpride
New to the CF scene

 
Join Date: Feb 2008
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
mountainpride is an unknown quantity at this point
How do I have PHP check the database it is posting to, and
tell the user if the username is unavailable.
mountainpride is offline   Reply With Quote
Old 05-31-2008, 07:05 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:49 PM.


Advertisement
Log in to turn off these ads.