[Zope] - Best way to input validation

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


--pgp-sign-Multipart_Tue_Jan_19_17:20:47_1999-1
Content-Type: multipart/mixed;
 boundary="Multipart_Tue_Jan_19_17:20:47_1999-1"
Content-Transfer-Encoding: 7bit

--Multipart_Tue_Jan_19_17:20:47_1999-1
Content-Type: text/plain; charset=US-ASCII

>>>>> "Paulo" == Paulo Eduardo Neves <neves@inf.puc-rio.br> writes:

  Paulo> Is there a best way to validate input form data?

  Jim> What would you like to do if an input is not valid?

  Paulo> Probably show the same form and the completed data with an
  Paulo> error message indicating why some fields were not valid. At
  Paulo> least a message in portuguese, since most of my readers won't
  Paulo> understand the english error message.

  Paulo> I was thinking about coding a javascript library to validate
  Paulo> the data in the client, but I'm convinced now that it isn't
  Paulo> the way to go. If I use the "automatic" error handling
  Paulo> feature I *can't* properly (from a usability view) handle it.

A while back I wrote a javascript module that does exactly this -- it
automatically tries to validate the form elements on the
client-side. You still need to do validation on the server side (for
many many reasons), but I figured it was better to try and catch
things client-side. This reduces the likelihood of the user actually
seeing the "automatic" error from Zope while still letting you use the 
marshalling, which is nice.

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.

 - Andrew
-- 
#!/usr/bin/env python
print(lambda s:s+"("+`s`+")")\
('#!/usr/bin/env python\012print(lambda s:s+"("+`s`+")")\\\012')
print(lambda x:x%`x`)('print(lambda x:x%%`x`)(%s)')

--Multipart_Tue_Jan_19_17:20:47_1999-1
Content-Type: application/octet-stream
Content-Disposition: attachment; filename="BoboValidate.js"
Content-Transfer-Encoding: 7bit

// 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;
}

--Multipart_Tue_Jan_19_17:20:47_1999-1--

--pgp-sign-Multipart_Tue_Jan_19_17:20:47_1999-1
Content-Type: application/pgp-signature
Content-Transfer-Encoding: 7bit

-----BEGIN PGP MESSAGE-----
Version: 2.6.3i
Comment: If you don't know what this is, you can safely ignore it.

iQCVAwUBNqQkQj9oumhqYnjxAQGG6QQAu87h92BRV3+gRor1q6Fg02IvYAJd9qfP
VkselwtJR2DF8hme2+4F56IWMfZx0Cx43ZjuygkLNqEugQL+oqaF+TYmyckAZRo4
hdGgIqv2vRa7nXoHjug14U19lOXySLNh9adq0mmHBGbhq8VXdZ2GtsxsDkDRrvN0
/Dws22agUbc=
=bd0K
-----END PGP MESSAGE-----

--pgp-sign-Multipart_Tue_Jan_19_17:20:47_1999-1--