Location: PHPKode > scripts > Contact Form Killer > contact-form-killer/killercontact_files/functions.php
<?php
	
	function costFormat($num,$nodecimals=0) {
		if ($nodecimals == 1) {
			$decimals = 0;
		} else {
			$decimals = 2;
		}
		return number_format($num,$decimals,'.',',');
	}
	
	
	function ternary($current,$item1,$item2) {
		if ($current == $item1) {
			return $item2;
		} else {
			return $item1;
		}
	}
	
	function getResultDiv($value,$type='error') {
		// Formats successful or error results whether they are in an array or a snippet.
		if ($type == 'success') {
			$class = 'success-div';
		} elseif ($type == 'test') {
			$class = 'test-div';
		} else {
			$class = 'error-div';
		}
		if (is_array($value)) {
			for ($i = 0; $value[$i] != ''; $i++) {
				$result_div .= '<li>' . $value[$i] . '</li>';
			}
			if ($result_div != '') {
				$result_div = '<div class="' . $class . '"><ul>' . $result_div . '</ul></div>';
			}
		} else {
			if ($value != '') {
				$result_div = '<div class="' . $class . '">' . $value . '</div>';
			}
		}
		return $result_div;
	}
	
	function checkValidChars($string,$valid_chars) {
		$string_array = str_split($string);
		$valid_chars_array = str_split($valid_chars);
		$i = 0;
		while ($string_array[$i] != '') {
			if (!in_array($string_array[$i],$valid_chars_array)) {
				return false;
			}
			$i++;
		}
		return true;
	}
	
	if (!function_exists('str_split')) {
		function str_split($string, $split_length = 1) {
			return explode("\r\n", chunk_split($string, $split_length));
		}
	}
	
	function getValidation($add_edit,$name,$msg,$type,$value='',$action) {
		global $edit_action;
		global $add_action;
		global $error_div;
		global $_POST;
		global $_GET;
		if ($_POST['action'] == $action) {
			$do = 1;
		} 
		// No value
		if ($type == 'novalue') {
			if ($do == 1) {
				if (strlen($_POST[$name]) < '1') {
					$error_div .= getResultDiv($msg);
				}
			}
			$js_clause = 'form.' . $name . '.value == ""';
			return jsCheck($js_clause,$msg,$name);
		}
		
		// Number is less than
		if ($type == 'less_than') {
			if ($do == 1) {
				if ($_POST[$name] < $value) {
					$error_div .= getResultDiv($msg);
				}
			}
			$js_clause = 'form.' . $name . '.value < ' . $value;
			return jsCheck($js_clause,$msg,$name);
		}
		
		// Number is greater than
		if ($type == 'greater_than') {
			if ($do == 1) {
				if ($_POST[$name] > $value) {
					$error_div .= getResultDiv($msg);
				}
			}
			$js_clause = 'form.' . $name . '.value > ' . $value;
			return jsCheck($js_clause,$msg,$name);
		}
		
		// Value equals
		if ($type == 'equals') {
			if ($do == 1) {
				if ($_POST[$name] == $value) {
					$error_div .= getResultDiv($msg);
				}
			}
			$js_clause = 'form.' . $name . '.value == ' . $value;
			return jsCheck($js_clause,$msg,$name);
		}
		
		// Less Than String Length
		if ($type == 'strlen_less') {
			if ($do == 1) {
				if (strlen($_POST[$name]) < $value) {
					$error_div .= getResultDiv($msg);
				}
			}
			$js_clause = 'form.' . $name . '.value.length < ' . $value;
			return jsCheck($js_clause,$msg,$name);
		}
		
		// String Length
		if ($type == 'strlen') {
			if ($do == 1) {
				if (strlen($_POST[$name]) != $value) {
					$error_div .= getResultDiv($msg);
				}
			}
			$js_clause = 'form.' . $name . '.value.length != ' . $value;
			return jsCheck($js_clause,$msg,$name);
		}
		
		// Zip Code
		if ($type == 'zip') {
			$valid_chars = "0123456789";
			if ($do == 1) {
				if (strlen($_POST[$name]) != 5) {
					$error_div .= getResultDiv('Please enter 5 digits for the zip code');
				} elseif (!checkValidChars($_POST[$name],$valid_chars)) {
					$error_div .= getResultDiv('Please enter only digits for the zip code');
				}
			}
			$js_clause_1 = 'form.' . $name . '.value.length != 5';
			$js_clause_2 = '!ValidChars(form.' . $name . '.value,"' . $valid_chars . '")';
			return 
				jsCheck($js_clause_1,'Please enter 5 numbers for the zip code',$name) . 
				jsCheck($js_clause_2,'Please enter only numbers in the zip code',$name);
		}
		
		// Price
		if ($type == 'price') {
			$valid_chars = "0123456789.,";
			
			if ($do == 1) {
				$post_value = str_replace(',','',$_POST[$name]);
				if (!checkValidChars($post_value,$valid_chars)) {
					$error_div .= getResultDiv('Please enter only a number for ' . $msg);
				} elseif (strlen($post_value) > $value) {
					$error_div .= getResultDiv('Please enter a smaller value for ' . $msg);
				}
			}
			$js_clause_1 = 'form.' . $name . '.value.length > ' . $value;
			$js_clause_2 = '!ValidChars(form.' . $name . '.value,"' . $valid_chars . '")';

			return 
				jsCheck($js_clause_1,'Please enter no more than ' . $value . ' characters for ' . $msg,$name) . 
				jsCheck($js_clause_2,'Please enter only numbers for ' . $msg,$name);
		}
		
		// Number
		if ($type == 'number') {
			$valid_chars = "0123456789";
			if ($do == 1) {
				$post_value = str_replace(',','',$_POST[$name]);
				if (!checkValidChars($post_value,$valid_chars)) {
					$error_div .= getResultDiv('Please enter only a number for ' . $msg);
				} elseif (strlen($post_value) > $value) {
					$error_div .= getResultDiv('Please enter a smaller value for ' . $msg);
				}
			}
			$js_clause_1 = 'form.' . $name . '.value.length > ' . $value;
			$js_clause_2 = '!ValidChars(form.' . $name . '.value,"' . $valid_chars . '")';

			return 
				jsCheck($js_clause_1,'Please enter no more than ' . $value . ' numbers for ' . $msg,$name) . 
				jsCheck($js_clause_2,'Please enter only numbers for ' . $msg,$name);
		}
		
		// Phone Number
		if ($type == 'phone') {
			$valid_chars = "0123456789-() ";
			$value = 6;
			if ($do == 1) {
				if (!checkValidChars($_POST[$name],$valid_chars)) {
					$error_div .= getResultDiv('Please enter only a phone number for ' . $msg);
				} elseif (strlen($post_value) > $value) {
					$error_div .= getResultDiv('Please enter a smaller value for ' . $msg);
				}
			}
			$js_clause_1 = 'form.' . $name . '.value.length < ' . $value;
			$js_clause_2 = '!ValidChars(form.' . $name . '.value,"' . $valid_chars . '")';

			return 
				jsCheck($js_clause_1,'Please enter a valid phone number',$name) . 
				jsCheck($js_clause_2,'Please enter only a phone number for ' . $msg,$name);
		}
	
		// Duplicate
		if ($type == 'duplicate') {
			if ($do == 1) {
				$value_array = explode(':',$value);
				$table = $value_array[0];
				$column = $value_array[1];
				$content = $_POST[$name];
			}
		}
		
	}
	
	function jsCheck($clause,$msg,$name) {
		return '
			if (' . $clause . ') {
			 alert( "' . $msg . '" );
			 form.' . $name . '.focus();
				return false;
			}
		';
	}
	
	
	function getLink() {
		$int = rand(1,5);
		$link[1] = '<a href="http://www.impliedbydesign.com">Web Design</a> Tools by Implied By Design</a>';
		$link[2] = '<a href="http://www.impliedbydesign.com/contact-form-killer.html">Web Design Tools</a> by Implied By Design</a>';
		$link[3] = '<a href="http://www.impliedbydesign.com/1-articles/">Web Design Articles</a> and Free Tools by Implied By Design</a>';
		$link[4] = '<a href="http://www.impliedbydesign.com/web-design-services.html">Web Design Services</a> by Implied By Design</a>';
		$link[5] = '<a href="http://www.impliedbydesign.com/1-articles/">Free Articles</a> and Free Tools by Implied By Design</a>';
		return $link[$int];
	}
?>
Return current item: Contact Form Killer