Loading ...
Day demonstration of Form Helper (DAYf)

Introduction

The object of this page is to show how a real-world date-input scenario can be set-up quickly in the browser with rule checking, error messages and conversions done as an immediate part of the UI.

You can process date inputs as we did with the Simple demonstration but there are two characteristics of date inputs. Firstly they need roughly the same validation and secondly they often come in pairs or more at a time. DAYf reduces the complexity of these common patterns.

Obvious method — Hard work

We go through this to illustrate the issues. Feel free to type into the inputs.

Raw reading

You can't get more basic (or useless) than this.
Date purchased Type something in here to see action.
Note that nk (not known), E (End of time) and 12 Oct are allowed.
<form> <span class="brown">Date purchased</span> <input class=red id=purchDate size=12 onKeyUp="DateVal(this);"> </form> <script> function DateVal(Elmt){ var dayo = new DAYo(Elmt.value); Try to create a DAYo object var ok = dayo.IsValid(); Useless in practice. $(Elmt).toggleClass('green',ok).toggleClass('red',!ok); Using jQuery here but YMMV } </script>
When we submit the form we'll be sending an unprocessed text string to the server. Of course we may be using an ajax call in which case we could do something like send dayo.To32Bits() or dayo.ToYmdhms('ARRAY'). It looks rather messy doesn't it.

Interlinked validation problem

Let's for the purposes of this exercise assume we have real bought and sold dates...
Except we might have bought it and still have it...
But if we've sold it then the sell date must be greater or equal to the buy date.
If your head hurts just thinking about it then we know the feeling. We can't escape the business rules but later we'll use DAYf to simplify implementing them.
Date purchased Date sold Don't mess with me!
Does form validate?

Note that the validation of the form and alert for inadmissable interaction between the dates are different.

Look at that mess of code!. This is a simple example and already we're plunged into validation hell. That's why DAYf is important.

One of the things we should address is user assistance that is a bit more streamlined and subtle.

function DateVal2(Elmt){ var dayo = new DAYo(Elmt.value); var ok = dayo.IsSpecific(); Different test. Needs Y,M and D $(Elmt).toggleClass('green',ok).toggleClass('red',!ok); DateCompare(); Interaction logic IsFormOk(); Validation bottom-line } function DateCompare(){ var dayB = new DAYo($('#buyDate').val()); var dayS = new DAYo($('#sellDate').val()); var showErr = true; if(dayB.IsSpecific()!==true){ showErr=false; }else{ It's getting very fiddly isn't it :( if(dayS.IsSpecific()===true){ if((dayB.SortsBefore(dayS))||(dayB.IsSameValue(dayS))){ showErr=false; } }else{ Perhaps no date at all if($('#sellDate').val().trim()===''){ showErr=false; } } } $('#errMsg').toggleClass('hideErr',!showErr); Rather crude! } function IsFormOk(){ Better make sure before POSTing etc. var ok= $('#buyDate').hasClass('green') && $('#sellDate').hasClass('green') && $('#errMsg').hasClass('hideErr'); $('#formOk').toggleClass('tick0',!ok).toggleClass('tick1',ok); }

Doing more common tasks the hard way

We'll have a go at this 'by hand' to illustrate the issues.
Date purchased Valid date needed" Help

Normally hidden INPUT value: -1