Skip to main content

Posts

Showing posts from September, 2010

Solid Basic Javascript Input Validation

Here is a simple yet pretty powerful and easy to use way to do validation on HTML form text boxes and text areas and similar controls. It checks to make sure required form field have *something* in them before allowing processing to continue. It’s a good simple way to add some basic validation to prevent trusted individuals from forgetting to fill out required fields. This is generally useful for internal websites where you can assume the user isn’t trying to hack the site. First we have two global JS variables that define the default and error-ed state of text boxes in question. Below I have standard controls with black borders and error-ed controls with red borders. // Assign a default border color for all form controls var defaultControlBorderColor = '#000000'; // Use a special color to highlight trouble (such as if a control doesn't validate) var notificationControlBorderColor = '#ff0000'; Below I have the primary function that does the field validation. F...

Execute a console command in PHP revised

While it seems like there are multitude of different methods to execute commands in PHP, so far the code I will go over below is my preferred method. Using the passthru() function works nicely and includes the final return code of whatever was being executed, which is generally useful to me in error checking. In processing, I also include ob_start(), ob_get_contents(), and ob_end_clean() so that I can capture the console programs output and return it the user eventually. // Execute a console command and return both the // command's success value and any output to the console it made function executeConsoleCommand($commandToExecute, &$commandConsoleOutputToAppend) { $returnValue = 0;      // Append command to output $commandConsoleOutputToAppend .= $commandToExecute . "\r\n"; // Catch console output ob_start(); // Execute the console program passthru($commandToExecute, $returnValue); // Save console output to a string variable (this s...

.NET Path.Combine() in PHP

I’m the type of person who prefers to do things in a way that I perceive as the proper or correct way. While this tends to make things take a bit longer to accomplish, I feel the result ends up being better overall. One such thing I noticed is that file and path handling in PHP was missing a function I used quite a bit when writing C# code. In C# there is a Path.Combine() function that appends two file paths together to form one complete path without having to care what the beginning character and ending character of the two paths are. I wrote up a little function in PHP to mimic what happens. // Take a path and/or a filename and combine them into one file path function filePathCombine($path1, $path2) { $completedPath = ''; // Check if path1 ends with DIRECTORY_SEPARATOR if (substr($path1, -1) !== DIRECTORY_SEPARATOR) { $completedPath = $path1 . DIRECTORY_SEPARATOR; } else { $completedPath = $path1; } // Check if path2 starts with D...