[Zope] javascript from Andrew Snare

François-Régis CHALAOUX fchalaou@strasbourg.synthelabo.fr
Fri, 30 Apr 1999 08:53:34 +0100


About javascript client-side checks.

FR

<<<<<<<<<<<<<
Date: 19 Jan 1999 17:20:50 +1100
From: Andrew Snare ajs@pigpond.com

Last time I posted this (on the Bobo list) there wasn't much of a
reaction, but maybe there will be this time. If anyone decides to use
it, please note that the module doesn't know how to validate some
:tags since I only implemented the easy ones and those which I needed
myself.


// This is a set of generic JavaScript routines designed to validate form
// input. Validation is performed for each text field based on the name
// of the field including a "tag". Inspired by the automatic type-conversion
// done by Bobo (http://www.digicool.com/releases/bobo)
//
// Author: Andrew Snare (ajs@pigpond.com)
//   Date: 5th September, 1998

// Each function in this group is called as required to verify fields of
// a specific type.

function text2float(element) {
    // Test for anything unparsable as a number.
    if (isNaN(parseFloat(element.value))) {
	alert("You have not entered a required number.");
	element.focus();
	return false;
    }
    return true;
}

function text2int(element) {
    // Test for anything unparsable as a number, or that has decimal places.
    if (isNaN(parseInt(element.value)) ||
	(parseInt(element.value) != parseFloat(element.value))) {
	alert("You have not entered a required whole number (without fractions or decimals).");
	element.focus();
	return false;
    }
    return true;
}
text2long = text2int;

function text2string(element) {
    // Anything can be represented as a string.
    return true;
}

function text2required(element) {
    // Just test for non-empty string.
    if (element.value + "" == "") {
	alert("You have not specified some required information.");
	element.focus();
	return false;
    }
    return true;
}
      
// This is a list of all the types we know how to verify. These correspond
// to the functions above.

conversionList = new Array();
conversionList[0] = "float";
conversionList[1] = "int";
conversionList[2] = "long";
conversionList[3] = "string";
conversionList[4] = "required";

// Call this function to validate as many of the text fields in the given
// form as possible. Suitable for use as:
//
//     <FORM .... onSubmit="return Validate(this);">

function Validate(form) {
    for (var i=0; i<form.elements.length; i++) {
	with (form.elements[i])
	    {
		if (type == "text") {
		    var lio = name.lastIndexOf(":");
		    if (lio != -1 && lio < (name.length-1)) {
			var conversion = name.substring(lio+1,name.length);
			var canconvert = false;
			var j = 0;
			while (!canconvert && j<conversionList.length) {
			    if (conversionList[j] == conversion) {
			     	canconvert = true;
				break;
			    }
			    j++;
			}
			if (!canconvert) {
			    break;
			}
			if (!eval("text2"+conversion+"(form.elements[i])"))
			    return false;
		    }
		}
	    }
    }
    return true;
}