static void

Javascript utilities

Core JS Libraries

Ok, these days I use JQuery, but these are my old javascript libraries. Some parts date back to 1996, and evolved over more than 10 years until JQuery started to take over.

mu.js- core stuff mostly for forms: see documentation

mu2.js- viewport operations, selectbox tools, stripeTable and setWait: see documentation.

mufx.js simple effects: Fade; Drag, msgBox, ToolTip, Preview. see documentation.


Encoding

Numeric Only

You can use this with the keydown event (cancelled by preventDefault) or keypress (cancelled by return false). This version allows tab, arrow keys (up increases the number, down lowers it), numeric keypad (96-105, not valid in keypress) and hitting the enter key submits the form.

if (!self.mu) var mu = {}; //create namespace if it doesn't exist
mu.numericOnly = function (e) {
    if (!e) var e = window.event;
    var key = e.which || e.keyCode;
    if(key == 9) return true; //tab
    if(key == 8  || key == 46) return true; //backspace - delete
    if(key == 35 || key == 36) return true; //home - end
    if(key == 13) {
        document.forms[0].submit(); //submit the form
        return false;
    }
    var src;
    if (e.target) src = e.target;
    else if (e.srcElement) src = e.srcElement;
    if (src.nodeType == 3) src = src.parentNode;//Safari bug
    if(key == 38) { //up
		var v = parseInt(src.value, 10) +1;
		src.value = v;
    }
    if (key == 40) {//down
		var v = parseInt(src.value, 10) -1;
		if(v>-1) src.value = v;
       }
    if (key >= 37 && key <= 40) return true; //arrowkeys are okay
    if (key > 47 && key < 58) return true; //0-9
    if (e.type != 'keypress' && key > 95 && key < 106) return true; //0-9 keypad (DO NOT USE if keypress)
	//anything else- invalid
	e.returnValue = false;
	if (e.preventDefault) e.preventDefault();
	return false;
};

Reference: ppk

"Unobtrusive" javascript validation

Here's a script that dynamically attaches events to all input fields with classes of "Mandatory", "Number" and "Date" to validate them. If there is an error it attaches a class of "Error"- otherwise it will format the value.

formValidator.js. Dependent on mu.addEvent (see mu.js above, but other addEvents could be substituted)

Calendar-pickers

I've used the dynarch one for several years, but now I mostly use the JQueryUI version. My lightweight version based on the origincal Marc Grabanski one is nice and quick with no dependencies.

Modified Clean-Calendar DatePicker

Height/Weight Conversion

HWConvert converts metric to Imperial heights and weights (UK version- stones and pounds). It uses the onchange event to update the related fields, and sets the class "Error" if applicable. There's a HWConvert.FieldIDs object that holds the IDs of the fields.

Height
Weight

Timesheet

My simple timesheet calculator.