Made of Everything You're Not

Professional(?!?!) blog of Eric Lamb.
  • Home
  • Projects
  • Portfolio
  • Resume

Posts Tagged ‘validation’

HTML_QuickForm Validation Functions

Posted in Code, Programming on April 3rd, 2009 by Eric Lamb – Be the first to comment

I’ve been using HTML_QuickForm for, what feels like, forever. I’m happy to say that I haven’t, manually, written a form in years. God, do they suck to write….

I may actually be in love with HTML_QuickForm.

HTML QuickForm

HTML QuickForm

Sigh…

Not everyone like HTML_QuickForm but it meets my needs pretty well. I don’t work with any php framework, I use Smarty extensively (QuickForm fits like a glove with Smarty) and have yet to get hung up on the crossover between business and design logic with it.

It is lacking some core validation functionality though so in the spirit of open source here are just a few of the validation functions I’ve written; free and clear.

NOTE: I’m assuming you already know about HTML_QuickForm already. If you aren’t already familiar, the below functions just won’t mean that much to you.

In the below are QuickForm ready validation functions that do the following:
1. Validate US PhoneNumber
2. Look up a URL
3. Check IP
4. Ensure a date isn’t set to the past
5. Ensure MySQL date format
6. Ensure positive number
7. Check decimal on number
8. Ensure a URL is valid
9. Make sure a file was uploaded (good for multi “file” field forms)

<?php
/**
 * Validation Functions
 *
 * Contains all the system validation.
 *
 * @author Eric Lamb <eric@ericlamb.net>
 * @version 1.0
 * @copyright	@author
 * @filesource
 * @link http://blog.ericlamb.net
 * @copyright 2005-2009 Eric Lamb
 */
 
/**
 * Ensures phone numbers are in the proper format
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	value of form field to check
 * @return  bool
 */
function CheckUSPhoneNum($element_name, $element_value)
{ 
	$PhoneNumber = ereg_replace("[^0-9]", "", $element_value); // Strip out non-numerics
	if(ereg("^([2-9][0-9]{2})([2-9][0-9]{2})([0-9]{4})$", $PhoneNumber, $NumberParts)){
		return "(" . $NumberParts[1] . ") " . $NumberParts[2] . "-" . $NumberParts[3];
	} else {
		return false;
	}
}
 
/**
 * Looks up a domain and makes sure it's valid; only works on *nix
 *
 * @param   string  $type	Type of banning to check
 * @param   string  $data	The cooresponding data to check
 * @return  bool
 */
function FormDomainLookup($element, $value, $arg) { 
    $value = str_replace('http://','',$value);
	exec("host -t ns $value",$hasil); 
    if (ereg("host $value not found.",strtolower(trim($hasil[0])))) { 
        return false; 
    } else { 
        return true; 
    }
}
 
/**
 * Makes sure an IP address is valid
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	IP Address to check
 * @return  bool
 */
function CheckIP ($element_name,$element_value) {
 
	$ip = $element_value;
	if (($longip = ip2long($ip)) !== false)
	{
		if ($ip == long2ip($longip))
		{
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	} 
}
 
/**
 * Makes sure a "date" isn't set to a past date.
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	Date to Check
 * @return  bool
 */
function StopPastDate($element_name, $element_value){
	global $vars, $now;
 
	//check that it's a valid date first
	$temp = explode('-', $element_value);
	if(!checkdate($temp['1'],$temp['2'],$temp['0'])){
		return false;
	}
 
	//now make sure the date isn't in the past.
	if($element_value < $now){
		return false;
	} else {
		return true;
	}
 
}
 
/**
 * Makes sure a "date" is a valid MYSQL format.
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	Date to Check
 * @return  bool
 */
function ValidateMysqlDate($element_name, $element_value){
	global $vars, $now;
 
	//check that it's a valid date first
	$temp = explode('-', $element_value);
	if(!checkdate($temp['1'],$temp['2'],$temp['0'])){
		return false;
	}
	return true;
}
 
/**
 * Makes sure an integer is positive
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	Int to Check
 * @return  bool
 */
function IsIntPositive($element_name, $element_value){
 
	if(!is_numeric($element_value){
		$element_value = (int)$element_value;
	}
	if($element_value < 1){
		return FALSE;
	}
	//exit;
 
	return TRUE;
}
 
/**
 * Makes sure an integer isn't a decimal
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	Int to Check
 * @return  bool
 */
function IsIntDecimal($element_name, $element_value){
 
	if(!is_numeric($element_value){
		$element_value = (int)$element_value;
	}
 
	if(strpos($element_value,'.') === FALSE){
		return TRUE;
	}
	//exit;
 
	return FALSE;
}
 
/**
 * Makes sure a URL is valid
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	URL to Check
 * @return  bool
 */
function IsURLValid($element_name,$element_value){
	if (preg_match("/^(http(s?):\/\/|ftp:\/\/{1})((\w+\.){1,})\w{2,}$/i", $element_value)) {
		return TRUE;
	} else {
		return FALSE;
	}
}
 
/**
 * Makes sure at least one file was uploaded
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	
 * @return  bool
 */
function EnsureUploadedFile($element_name, $element_value){
	foreach($_FILES AS $name => $data){
		if($data['error'] == '0'){
			return TRUE;
		}
	}
	return FALSE;
}
?>
Bookmark and Share
  • Subscribe: Entries | Comments
  • About Me

    Email Email
    Twitter Twitter
    310.739.3322
  • Categories

    • Brain Dump
    • Business
    • Code
    • IT
    • Programming
    • Rant
    • Servers
  • Archives

    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008

Copyright © 2008 - 2010 Eric Lamb - All rights reserved