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.
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);
}