Monday, October 13, 2008

Disable form submit on enter key press in (almost) one line of javascript

How to prevent auto-submission of forms with (almost) one line of javascript...

You make your web form and you test filling it out, and you find out hitting enter causes the form to automatically submit, when in fact you want a different behavior. Here's the simplest, cleanest, most compatible way to do this in browser-agnostic javascript without any dependencies of any kind:

<body onkeypress="var e = (event ? event : window.event); return (e.keyCode ? e.keyCode : e.charCode ? e.charCode : e.which) != 13;" >

Essentially that is saying grab the event, determine which key was pressed, and return false if it was the enter key. Returning false suppresses the form from auto-submitting.

Suppose you have a really super useful event handler like the following:

<input type='text' onkeypress='alert("Wow this sure is annoying");' />

The alert would still be shown even though you have the enter key-eating code at the body level thanks to the bubbling precedence of event handling. In essence, the onkeypress event handler is called for the textbox before the event handler code for the body is called. (If you return false from the textbox, it would never bubble up to the body's event handler)

This has been tested on the various current versions of IE (6,7,8), Firefox (2,3), and Safari (3).

Friday, January 11, 2008

2 - Not able to initialize admin object definitions against db ...

So I was tasked with automating the creation and deletion of BlackBerry users. According to RIM, the recommended way of doing this is by using their BlackBerry Resource Kit -- namely the BlackBerry User Admin Service and Tool.

I've never done anything with BlackBerry before, so needless to say I spent some time making noob errors when trying to install the Admin Service. First off, I was getting this funky error:

MAPILogonEx() failed (80040111) for profile:

Okay, that's easy enough to see what's going on. A missing or invalid MAPI profile. I needed a MAPI profile locally (dammit Jim, I'm a developer, not an admin!). I created one using a tool from Microsoft's website:

I eventually did get to a sticking point when google no longer helped:

"2 - Not able to initialize admin object definitions against db"

I ran SQL profiler to make sure the service was successfully connecting to the proper database (BESMgmt) and it had rights to do everything -- yep everything was cherry. Still no love though. It seemed to point at a database-related error. I fired up Query Analyzer and looked for a table relating to 'object definitions'. Sure enough, an ObjectDefn table existed. Here's a partial sample record:

<AdminObject name="ServerConfig" dbtable="ServerConfig"><Property clientname="Id" dbname="Id" type="56" length="4" isnullable="0" key="1" /><Property clientname="ServiceName" dbname="ServiceName" type="231" length="512" isnullable="1" /><Property clientnam...

Then it dawned on me: they're storing xml in the database. I didn't verify that msxml 4.0 was installed (per the Admin User Documentation from RIM). So I grabbed MSXML 4.0 from Microsoft's website and installed it. Sure enough, the service could finally start. Yay.

Moral of the story: verify your prereq's before banging your head against the wall.

Long story short:
2 - Not able to initialize admin object definitions against db == wrong version of MSXML installed

Hopefully somebody googles this and doesn't have to recreate my madness :-)