hi, i cant seems to get my valid URL to work. it keeps showing invalid URL even i enter a valid one. why? part 2 of the coding is at the bottom
CValidate.php
<?php
class validator
{
var $error, $error_message; // (string) HTML formatted error message
var $error_array = array(); // (array) Error
function validate_fields($required_fields='')
{
if (!$required_fields)
{
return true;
}
$__errors = array();
// Delete all spaces from the field names and place the them in an array.
$__fields = explode (",", str_replace(" ", "", $required_fields));
foreach ($__fields as $__current_field)
{
if (trim($__current_field))
{
if (strstr($__current_field, "||"))
{
/* * * * "OR" fields * * * */
// this_field||that_field - double pipe separated field names will check <this_field> or <that_field>
$__error = false;
$__no_error = false;
$__sub_fields = explode("||", $__current_field);
foreach ($__sub_fields as $__current_sub_field)
{
$__current_sub_field = trim($__current_sub_field);
settype($_REQUEST[$__current_sub_field], "string");
if (!$__no_error && isset($_REQUEST[$__current_sub_field]) && !strlen(trim($_REQUEST[$__current_sub_field])))
{
$__error = true;
}
else
{
$__no_error = 1;
$__error = false;
}
}
if ($__error)
{
$__errors[] = $__current_field;
}
}
else
{
/* * * * Regular fields * * * */
// This separates regular single fields and makes them global variables
$__current_field = trim($__current_field);
settype($_REQUEST[$__current_field], "string");
if (isset($_REQUEST[$__current_field]) && !strlen(trim($_REQUEST[$__current_field])))
{
$__errors[] = $__current_field;
}
}
}
}
if (count($__errors))
{
$this->create_error($__errors, "validate_fields");
return FALSE;
}
else
{
return TRUE;
}
} // end of function validate_fields()
function validURL( $url )
{
if ( !preg_match( '!^((ht|f)tps?\:\/\/)?[a-zA-Z]{1}([\w\-]+\.)+([\w]{2,5})/?$!i', $url ) )
{
$this->create_error("", "invalidURL");
return FALSE;
}
else
{ return TRUE; }
}
function create_error($error, $type='')
{
$this->error = ereg_replace("<br>$", "", $this->error);
if($type=="invalidURL")
{
$r2="<b>Invalid URL</b>";
$this->error=$r2;
}
if ($type == "validate_fields")
{
$r = "<b>Please fill in the following fields:</b><br>\n";
foreach ($error as $v)
{
$i = 1;
$r .= " • ";
$v_array = explode("||", $v);
foreach ($v_array as $c)
{
if (trim($c))
{
if ($i > 1) { $r .= " <b>or</b> "; }
$missing_fields[] = $c;
$r .= ucwords(eregi_replace("_", " ", $c));
$i++;
}
}
$r .= "<br>\n";
}
$this->error .= $r;
$this->error_array['missing_fields'] = $missing_fields;
}
if ($type == "message")
{
if (!$this->error_array['message'])
{
$this->error .= "<b>The following errors occured:</b><br>\n";
}
$this->error .= " • " . $error . "<br>\n";
$this->error_array['message'][] = $missing_fields;
}
$this->error .= "<br>";
$this->error_message = $this->error;
}//end of function
}
?>