[Zope] Validating data entry (dates)
Jim Sanford
jsanford@atinucleus.com
Thu, 9 Dec 1999 16:42:05 -0600
HERE IS SOME JAVASCRIPT I USE TO VALIDATE DATES ON THE CLIENT SIDE
function JValiDate(dateStr){
if (dateStr.length==0){
dateStr='99/99/9999';
}
else if (dateStr.charAt(0)==' '){
dateStr='99/99/9999';
}
while (dateStr.charAt(dateStr.length-1)==' ') dateStr=dateStr.substring(0,dateStr.length-1);
var dtmp='';
var z=''
for (var x=0; x < dateStr.length; x++) {
z=dateStr.charAt(x);
if (z>=0 && z<=9) {
dtmp+=z;
}
else dtmp+='/';
}
if (dtmp.charAt(1)=='/') dtmp='0'+dtmp;
if (dtmp.charAt(4)=='/') dtmp=dtmp.substring(0,3)+'0'+dtmp.substring(3,dtmp.length);
// alert ('>'+dtmp+'<');
// alert (dtmp.length);
if (dtmp.length==8){
tyear=dtmp.substring(6,8);
// alert(tyear)
if (tyear<31) {
dtmp=dtmp.substring(0,6)+'20'+tyear
}
else {
dtmp=dtmp.substring(0,6)+'19'+tyear;
}
}
var a=dtmp
var err=0
var psj=0;
if (a.length != 10) err=1
var b = a.substring(0, 2)// month
var c = a.substring(2, 3)// '/'
var d = a.substring(3, 5)// day
var e = a.substring(5, 6)// '/'
var f = a.substring(6, 10)// year
// basic error checking
if (b<1 || b>12) err = 1
if (c != '/') err = 1
if (d<1 || d>31) err = 1
if (e != '/') err = 1
if (f<1900 || f>2099) err = 1
// advanced error checking
// months with 30 days
if (b==4 || b==6 || b==9 || b==11){
if (d==31) err=1
}
// february, leap year
if (b==2){
// feb
var g=parseInt(f/4)
if (isNaN(g)) {
err=1
}
if (d>29) err=1
if (d==29 && ((f/4)!=parseInt(f/4))) err=1
}
if (err==1){
if (dateStr!='99/99/9999'){
alert('Date '+dtmp+' is not valid!');
}
return ''
}
else {
return dtmp;
}
}
HERE IS EXAMPLE OF USE
<input type="text" name="wpodate" size="9" maxlength="10"
value="<dtml-var "podate.toZone('US/Central').strftime('%m-%d-%Y')">"
onChange="this.value=JValiDate(this.value)">
__________________________________________________________________
Jim Sanford
. Database Engineer
/ \ / Accelerated Technology, Inc.
/ / 720 Oak Circle Drive East
/ / \ Mobile, AL 36609
/ / \ Voice: 334-661-5770 fax: 334-661-5788
/ \ E-Mail: jsanford@atinucleus.com
Web: http://www.atinucleus.com
Source Code, No Royalties, Any CPU...It just make sense !
__________________________________________________________________
----- Original Message -----
From: Henny van der Linde <linde@inline-info.nl>
To: Zope mailing list <zope@zope.org>
Sent: Wednesday, December 08, 1999 5:13 PM
Subject: [Zope] Validating data entry (dates)
Hi,
Is there an elegant way to validate fields in a form before submitting.
For example:
I have a date field. I want to check, before submitting the form, if the
date entered is valid.
Is there someting like a function as: Date.IsValid (I haven a Delphi
background). What I don't want is a Zope generated standard error screen. I
want a custom designed error message/page.
thanks,
Henny van der Linde
_______________________________________________
Zope maillist - Zope@zope.org
http://lists.zope.org/mailman/listinfo/zope
No cross posts or HTML encoding!
(Related lists -
http://lists.zope.org/mailman/listinfo/zope-announce
http://lists.zope.org/mailman/listinfo/zope-dev )