Re: PHP / Regex help?

From: Date: Thu, 08 May 2003 09:25:32 +0000
Subject: Re: PHP / Regex help?
References: 1  Groups: php.install 
Request: Send a blank email to php-install+get-10843@lists.php.net to get a copy of this message
hi! allthough this is off-topic of this mailing list you would help us, if you said us where the validation fails. .ma The_polymorph <The_Polymorph@rocketmail.com> wrote@08.05.2003 8:01 Uhr: > Hi. > > I am passing several form fields to the script below but even when the > data *should* be valid, it is being 'tagged' as invalid. Very puzzling. > Any help would be appreciated: > > <?php > > > // 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 false; > } > > else { > return true; > } > } > > // 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 false; > } > > else { > return true; > } > } > > > // 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 false; > } > > else { > return true; > } > } > > // 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 false; > } > > else { > return true; > } > } > > // Validate major field. Must contain only alphabetic > // characters. If not, return false. Otherwise, return true. > function major( $major ) { > > if( eregi("[^a-z]+", $major ) ) { > return false; > } > > else { > return true; > } > } > > // Validate city field. Must contain only alphabetic > // characters. If not, return false. Otherwise, return true. > function city( $city ) { > > if( eregi("[^a-z]+", $city ) ) { > return false; > } > > else { > return true; > } > } > > // Validate state field. Must contain only alphabetic > // characters. If not, return false. Otherwise, return true. > function state( $state ) { > > if( eregi("[^a-z]+", $state ) ) { > return false; > } > > else { > return true; > } > } > > // 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 false; > } > > else { > return true; > } > } > > ?> > > > Thanks, > > -Katie > > ===== > 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 Matthias Steinböck Email: grillen@abendstille.at Web: http://www.abendstille.at Frühabendliches Webdesign. ------------------------------------------ Im Übrigen sind wir der Meinung, dass Wir im Fall der Fälle auch ihre Zehennägel schneiden, denn der Kunde ist König.

« previous php.install (#10843) next »