11-19-2007, 11:11 AM
|
#6 (permalink)
|
|
The Wanderer
Join Date: Nov 2007
Location: according to my wife: on the Net
Posts: 19
Thanks: 0
|
Hi Matt,
This is a good example form submission script, but I would like to suggest you use both HTML and TEXT e-mails, since I know that many companies disable receiving html-rich mails. By providing a text-based alternative, these recipients will be grateful.
As a paranoid developer, I always check input fields on their types and lenghts, so in case of your name field I check the lenght is between 2 and 50 (max size db field), that the field is alphanumeric (no one calls his son or daughter example_123) and that no html or db scripts can be run.
Most of the time I use Zend Framework to check this, but a simple class can do this job for you:
Code:
<?php
/**
* Simple class to validate form input.
*/
class formValidator
{
/**
* Check wether a value is alpha-numeric, if it consists only of
* characters.
*
* @param mixed $value
* @return boolean
*/
public function isAlpha($value)
{
return ctype_alpha($value);
}
/**
* Check wether a value is numeric, if it consists only of
* numbers.
*
* @param mixed $value
* @return boolean
*/
public function isNum($value)
{
return ctype_digit($value);
}
/**
* A simple e-mail address validation checker, to see if the
* entered e-mail address is correct.
*
* @param string $value
* @return boolean
*/
public function isEmail($value)
{
$valid = false;
$match = array();
$pattern = "/^[a-zA-Z0-9\-\_\.]+\@[a-z0-9\-\_\.]+\.[a-z]{2,5}$/";
preg_match($pattern, $value, $match);
if (key_exists(0, $match) && strcmp($match[0], $value) === 0) {
$valid = true;
}
return $valid;
}
/**
* Check wether a sumitted text is between a minimum length and
* a maximum length. Defaults are minimum 2 and maximum 50 chars.
*
* @param mixed $value
* @param int $min
* @param int $max
* @return boolean
*/
public function isBetween($value, $min = 2, $max = 50)
{
return strlen($value) >= $min && strlen($value) <= $max ? true : false;
}
}
/*
// Example script to see it's functionality
$fv = new formValidator();
echo $fv->isAlpha("Lorum");
echo $fv->isNum("123");
echo $fv->isBetween("We ar champions!");
echo $fv->isEmail("john_123.doe@sub-domain.example.com");
*/
|
|
|