Jump to content

validator class


Naez

Recommended Posts

[code=php:0]
<?php
class validator
{
    public function __construct()
    {
    }

    public function validateLength($string, $minlength=0, $maxlength=null)
    { 
        if (strlen($string) >= $minlength && strlen($string) <= $maxlength) return true;
        return false;        
    }
    
    public function validateEmail($email, $minlength=7, $maxlength=30, $regex="/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/")
    {
        if (!$this->matchRegex($email,$regex) || !$this->validateLength($email, $minlength, $maxlength)) return false;
        return true; 
    }
    
    public function matchRegex($string, $regex)
    {
        if (!preg_match($regex, $string)) return false;
        return true;           
    }    

}
?>

[/code]

 

 

Just taking a quick look at my code are there any methods you would add that I just didn't think of (or even change from what I've done).

Link to comment
Share on other sites

<?php
   public function matchRegex($string, $regex)
    {
        if (!preg_match($regex, $string)) return false;
        return true;           
    }
?>

 

First thing is I'd reverse $regex and $string in your arguements - better to stick with the same order when making your own function to emulate/execute a php function. Eg when connecting to mysql use the same order of host,user,password.

 

But I don't even know why you'd want this function, it's not any harder to remember preg_match then matchRegex, and preg_match is a function most programmers are already familier with.

 

You have a length check to check the length of a string, what about a range check to check numbers are within a certain range?

eg

<?php
public function rangeCheck($num, $min, $max) {
if (($num < $min) OR ($num > $max)) return true;
return false;
}
?>

 

Other checks. Presence checks? Make sure the field is set in some way

<?php
public function presenceCheck($var)
{
  if(isset($var)) return true;
  return false;
}
?>

Not really necessary in PHP since isset is built in, but if you switch languages it's one to remember, and I usually include it anyway (since my debugging also stores errors in mysql when debug is on :D)

 

Type check? Make sure a variable is of a certain type using the following functions

# is_array — Finds whether a variable is an array

# is_binary — Finds whether a variable is a native binary string

# is_bool — Finds out whether a variable is a boolean

# is_buffer — Finds whether a variable is a native unicode or binary string

# is_callable — Verify that the contents of a variable can be called as a function

# is_double — Alias of is_float

# is_float — Finds whether the type of a variable is float

# is_int — Find whether the type of a variable is integer

# is_integer — Alias of is_int

# is_long — Alias of is_int

# is_null — Finds whether a variable is NULL

# is_numeric — Finds whether a variable is a number or a numeric string

# is_object — Finds whether a variable is an object

# is_real — Alias of is_float

# is_resource — Finds whether a variable is a resource

# is_scalar — Finds whether a variable is a scalar

# is_string — Find whether the type of a variable is string

# is_unicode — Finds whether a variable is a unicode string

I'd use

<?php
public function typeCheck($var,$type)
{
switch ($type)
{
  case "number" :
   if(is_numerical($var)) return true;
  break;
  case "integer" :
   if(is_int($var)) return true;
  break;
  case "string" :
   if(is_string($var)) return true;
  break;
  default :
   //Do whatever you want to do if you don't have a check for this variable
   #return false anyway
   return false;
   #or maybe return the var type
   return gettype($var);
  break;
}
#Not true, therefore false.
return false;
}
?>

And just include any types you want.

(Note, I think you can just use return is_numerical($var); - saves a line or two of code)

 

You might also want a function to check usernames/posts against either

-A regexp of words not allowed

-A mysql table of words not allowed

 

I don't know what other fields you might use, but for things like number plates, phone numbers, ZIP/postal codes. Think along the lines of your email check - what other fields have a set format. Some things (eg forum posts) don't, but others will always be x letters followed by x numbers and a pirate flag, or whatever.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.