Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts

    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.

  • #2
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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).

  • #3
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    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.

  • #4
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts
    How do I have PHP check the database it is posting to, and
    tell the user if the username is unavailable.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •