Jim Rogers

Lives in Baton Rouge, LA, with two dogs, one cat, and one lovely wife. I'm a lead developer for GCR Incorporated.

Katrin and Jim

Month List

Checking for a ReCAPTCHA entry on the client

by Jim Feb 27, 2012 9:06 AM

ReCAPTCHA necessarily posts back to the server for validation of the entered text. However, if no text has been entered at all, then there's nothing to post back for. In ASP.NET web forms, we can use a CustomValidator control to check whether there's anything to submit, and if not, avoid the round trip to the server.

This example uses a ValidationSummary control (not shown.)

<asp:CustomValidator runat="server" ID="valCaptchaEntered" 
  ErrorMessage="Please complete the verification code (captcha)" 
  Display="None" ClientValidationFunction="validateCaptchaEntered">
</asp:CustomValidator> 
<div id="recaptcha_div"> 
<
recaptcha:RecaptchaControl ID="capLogin" runat="server"
PublicKey="[key]" PrivateKey="[key]"
Theme="white" CssClass="captcha" OverrideSecureMode="true" />
</
div>

Assuming you're using jQuery, the client side validation function is just a couple of lines, and looks like this:

// Make sure the user entered something into the captcha, so we don't round-trip to the 
// server if they've left it empty.
function validateCaptchaEntered(source, arguments) { var value = $.trim($("#recaptcha_response_field").val()); arguments.IsValid = (value != ""); }

JSON Parser in AS3

by Jim Jan 31, 2012 9:22 PM

In preparation for creating a JSON parser that could parse directly to model objects, I created this generic parser. I’m not going to use it, because it’s painfully slow (30 seconds to parse vs. a fraction of a second for the built-in parser.) So it goes on the blog  :-P

It’s somewhat simplified from the full algorithm, but it handles basic cases and could be up to the standard with a little more work.

package util
{
   import flash.utils.*;
   
   public class JSONParser
   {
      private var _data : String;
     
      private var _index : int = 0;
     
      public function JSONParser(data : String)
      {
         _data = data;
      }
     
      // get one character and advance
      private function take() : String
      {        
         return _data.charAt(_index++);
      }

      private function assert(condition : Boolean) : void
      {
         if (!condition)
            throw new Error("Unexpected character at position " + _index.toString());
      }
     
      // assign values to properties until we reach a matching end bracket
      private function assignProperties(target : Object) : void
      {
         // we've just entered an object, should be getting a field name first...
         var field : String;
         
         while (true)
         {
            field = "";
           
            assert(take() == "\"");    // field should start with quote
            var char : String = take();
            while(char != "\"")
            {
               field += char;
               char = take();
            }
           
            assert(take() == ":");
           
            target[field] = getValue();
           
            // having got the value, next char should be a comma or
            // the end bracket we're looking for
            char = take();
            if (char == "}")
               break;
            assert(char == ",");
         }
      }
     
      // Next thing in line is the value of a field; return that
      private function getValue() : Object
      {
         // might be a simple type, an object, or an array...
         var char : String = take();

         if (char == "\"")
            return readToQuote();

         if (char == "{")
         {
            // this value is itself an object
            var value : Object = new Object();
            assignProperties(value);
            return value;
         }
         else if(char == "[")
         {
            // We've got an array of objects
           
            var ar : Array = new Array();
            var element : Object;
            char = ",";          // fake to get in loop
            while (char == ",")
            {
               element = getValue();
               ar.push(element);
               char = take();
            }
            assert(char == "]")     // should have been end of the array
            return ar;
         }
         else
            assert(false);
         return null;
      }
   
      // read a string literal until we get to the next quote (eats the end quote)
      private function readToQuote() : String
      {
         var end : int = _data.indexOf("\"", _index);
         var s : String = _data.substring(_index, end);
         _index = end + 1;
         return s;
      }
     
      public function parse() : Object
      {
         var char : String;
         var root : Object;
         
         assert(take() == "{");
         
         root = new Object();
         
         assignProperties(root);
         
         return root;
      }
   }
}

2011 EOS Receiver Hitch

by jim Jan 24, 2012 8:31 PM

A few months ago I got a new VW Eos. I might have purchased the last manual transmission EOS in the U.S. - certainly in the southeast. It’s a shame they discontinued that option in the 2012 U.S. market.

There isn't a whole lot of information out there about bike racks on the American EOS, so I thought I would share my experience and some pictures, for anyone else that's wondering about this.

I got a Draw-Tite Sportframe receiver - this is rated at 200lb tongue weight and 2000lb towing.
http://www.etrailer.com/Trailer-Hitc...eid=2011208575.

For the rack I chose a Swagman XTC-2.

Some things about the install:
I had to jack up the passenger side a bit to get the receiver in place.

On my car, the passenger side bolt spacing is apparently non-standard. I couldn't put the exhaust hanger bracket bolts back into place. I called eTrailer, and they called Draw-Tite, and the spacing on the receiver is correct, at exactly the specified 4 inches. I guess there's some variability between cars, as to the exact spacing of the exhaust brackets. I expanded the rear passenger side bolt hole by about 1/4 inch with a dremel and grinder bit, to make everything fit.

I suggest checking this spacing before fishing the bolts into their holes with the provided wire tool. It’s not real hard to get the bolts into place, but it’s a real pain getting them back out again.

The receiver pushes up the rear fairing by about 3/4 inch. It's not really noticeable, because it's under the car, and it might be by design to keep it from vibrating.

The main bar of the receiver touches my trunk floor - in fact it was "in the way" by about 1/8 or 1/4 inch. I couldn't push the bolt flanges flush against the frame, and had to just torque the bolts down. Again I'm chalking that up to some variability in the car.

Some of the exhaust heat shield does need to be trimmed back, as the installation instructions say. The aluminum can be cut with a sturdy utility knife.

With the middle bar of the rack folded down, there's plenty of room for the top to open.

The bike closest to the car doesn't have a lot of pedal clearance, but the rack is very sturdy and I don't think it will flex enough for the pedal to hit the trunk. I'm going to put an sock over that anyway; I could bang it when putting the bike on the rack.

All in all, I'm very happy with the bike setup. The rack is sturdy, and easier to deal with than the roof rack I had on my Jetta.

Tags:

Tinkering

Removing a #region with Resharper

by Jim Sep 22, 2011 8:28 PM

Collasped

I hate opening up a source file and seeing that. No, not the VB.NET, though I’m not a fan of that either. I mean the fifteen region directives in a 312 line file. Most of those have one method in them. I guess at some point that was a coding standard or something - but dude, you’re making the code harder to read, not easier.

Neither Visual Studio nor resharper has a direct means of quickly removing individual region directives, though resharper can be configured to remove all of them when you reformat the file.

To remove specific regions, follow these steps:

Type Ctrl+Alt+F to display resharper’s File Structure window, shown below. The gray outlines correspond to regions.

Click the ‘x’ in the upper right corner of the region box. The start and end region directives will be removed, but the code they contain will remain.

image

An obscure DB2 error

by Jim Aug 17, 2011 9:06 AM

SQL1159 Initialization error with DB2 .NET Data Provider, reason code 7, tokens 9.5.0.DEF.2, SOFTWARE\IBM\DB2\InstalledCopies

For me the solution to this error was simply this: run my .NET website under IIS, rather than under Visual Studio’s development server. There are apparently other sources of this error, maybe related to x86 vs. x64 installations; IBM has some documentation of the error types.

If I had to guess, I would think that the IBM driver DLLs are trying to read from the registry, and the development server doesn’t have the necessary permissions. Reason code 7 is a registry error, and the tokens are an existing registry value and path under HKEY_LOCAL_MACHINE.

Tags: ,

Code