PHP / Regex help? ( solved ).
| From: | The_polymorph | Date: | Thu, 08 May 2003 22:10:33 +0000 |
| Subject: | PHP / Regex help? ( solved ). | ||
| Groups: | php.install | ||
| Request: | Send a blank email to php-install+get-10848@lists.php.net to get a copy of this message | ||
Hi all.
I apologize for the off-topic nature of this post but I felt obligated
to provide those with an interest in form validation the corrected
version of my code. The mistake I was making was quite minor and I am a
little embarrassed by overlooking it initially.
I merely had to change most of the eregi functions to:
if( eregi("^[character class]+$" ) )
Hope this helps,
-Katie.
<?php
// Version 1.1 of the form validator project.
// Define a manifest constant. Equate 'LOG' with the file.
define( "LOG", "survey_log.txt" );
// Initialize three empty arrays.
$form_errors = array();
$form_data = array();
$form_fields = array();
// Assign form variables to local variables.
// HTMLSpecialChars is a function that converts HTML tags
// to ordinary text so that won't be interpreted by the
// browser.
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_address = $_POST['email_address'];
$telephone = $_POST['telephone'];
$major = $_POST['major'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip_code = $_POST['zip_code'];
$contact = $_POST['contact'];
$comment = HTMLSpecialChars( $_POST['comment'] );
// Assign each of the form fields to the '$form_fields' array.
// This was done to increase efficiency.
array_push( $form_fields, "first_name", "last_name",
"email_address",
"telephone", "major", "city", "state",
"zip_code" );
// Check the value of each element in the $form_fields array.
// If it isn't blank and the contents match a specific pattern,
// assign the value to the $form_data array.
foreach( $form_fields as $field ) {
if( $field != "" && $field( $$field ) ) {
$form_data[] = $field;
}
// Assign the enclosed string to the '$form_errors' array
// if the field is either blank or the contents didn't
// match the individual pattern.
else {
$form_errors[] = "$field contains missing or invalid data<br/>";
}
}
// Does the '$contact' variable have a value that evaluates
// to true?
if( $contact == "contact" ) {
$form_data[] = "Requesting contact";
}
// Store the value of '$comment' in '$form_data'.
$form_data[] = "$comment";
// If there were errors in any of the form fields, loop
// through the '$form_errors' array and display each of
// them in sequence.
if( $form_errors ) {
echo "<center><h3>Form
errors</h3></center><br/><br/>";
foreach( $form_errors as $error ) {
echo "<font color=\"red\">$error<br/></font>";
}
}
// No errors in form data. If the user has stated they would
// like to be contacted ( via the checkbox ), display the
// string 'You will be contacted shortly'. Otherwise, simply
// thank them for their feedback.
else {
echo "<html><body
bgcolor=\"aquamarine\"><center><h3>Thank you for
your feedback.</h3></center></body></html><br/><br/>";
if( $contact ) {
echo "<html><body bgcolor=\"aquamarine\">You will be contacted
shortly.</body></html><br/>";
}
}
// If the 'survey_log.txt' file cannot be opened, display
// the string and exit.
if(!$file = fopen( LOG, "a+" ) ) {
echo "Could not open file " . LOG;
exit;
}
// If the file is open and there are no form errors present,
// write a time stamp for the submission to disk.
if( $file && (!($form_errors) ) ) {
$date_stamp = getdate();
$week_day = $date_stamp['weekday'];
$month = $date_stamp['month'];
$day = $date_stamp['mday'];
$year = $date_stamp['year'];
$hrs = $date_stamp['hours'];
$mins = $date_stamp['minutes'];
$secs = $date_stamp['seconds'];
fwrite( $file, "\nSubmitted: $week_day $month $day $year at
$hrs:$mins:$secs\n" );
// Iterate through the form data and write each of them
// to disk.
foreach( $form_data as $data ) {
fwrite( $file, "$data\n" );
}
}
// There were form field errors.
else {
echo "Could not send form data to the server!<br/>";
exit;
}
// Could not close file.
if( !fclose( $file ) ) {
echo "Error closing file " . LOG;
}
// Validate first_name field. Must contain only alphabetic
// characters. If not, return false. Otherwise, return true.
function first_name( $first_name ) {
if( eregi("^[a-z]+$", $first_name ) ) {
return true;
}
else {
return false;
}
}
// Validate last_name field. Must contain only alphabetic
// characters. If not, return false. Otherwise, return true.
function last_name( $last_name ) {
if( eregi("^[a-z]+$", $last_name ) ) {
return true;
}
else {
return false;
}
}
// Validate first_name field. Must contain only specific
// characters. If not, return false. Otherwise, return true.
function email_address( $email_address ) {
if( eregi( "^" .
"[a-z0-9]+([_\\.-][a-z0-9]+)*" .
"@" . "([a-z0-9]+([\.-][a-z0-9]+)*)+" .
"\\.[a-z]{2,}" .
"$", $email_address ) ) {
return true;
}
else {
return false;
}
}
// Validate telephone field. Must contain only numeric
// characters which are seperated by either a dash('-')
// or a period('.'). If not, return false. Otherwise, return true.
function telephone( $telephone ) {
if( eregi("^[0-9]{3}[.-][0-9]{3}[.-][0-9]{4}$", $telephone ) ) {
return true;
}
else {
return false;
}
}
// Validate major field. Must contain only alphabetic
// characters. If not, return false. Otherwise, return true.
function major( $major ) {
if( eregi("^[a-z]+$", $major ) ) {
return true;
}
else {
return false;
}
}
// Validate city field. Must contain only alphabetic
// characters. If not, return false. Otherwise, return true.
function city( $city ) {
if( eregi("^[a-z]+$", $city ) ) {
return true;
}
else {
return false;
}
}
// Validate state field. Must contain only alphabetic
// characters. If not, return false. Otherwise, return true.
function state( $state ) {
if( eregi("^[a-z]+$", $state ) ) {
return true;
}
else {
return false;
}
}
// Validate zip_code field. Must contain only five numeric
// characters. If not, return false. Otherwise, return true.
function zip_code( $zip_code ) {
if( eregi("^[0-9]{5}$", $zip_code ) ) {
return true;
}
else {
return false;
}
}
?>
=====
One of the key characteristics of professionalism is a loyalty to the profession that transcends
loyalty to any single employer.
-Andrew Koenig
__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com