Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Range

Status
Not open for further replies.

Phudsen

Technical User
Mar 7, 2003
136
0
0
A2
Hi,

I have a search form which I use to search the database for date range.

What I want is two sets of drop downs:

First one:
-----------
Select Month, Select Day, Select year.

When I select Feb for instance, I want the Day drop to list only 28 days, if I select December, the Day drop to list 31 days ...etc.

Second one:
------------
Same

This way I will have three drop down buttons on the first line and another three drop down buttons on the second line.

When I press submit button, I can handle this with ASP.

Thanks a lot
Paulin
 
the best thing i wud suggest you to do is to provide text box and let them enter the date in mm/dd/yyyy format and validate that date. it is easier for the programing language to query and use the string and work with it.
this might help
you said date range so i have used start_dt and end_dt as a range

<script language=&quot;javascript&quot;>

function isDate(dateStr) {

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
return false;
}

month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[5];

if (month < 1 || month > 12) { // check month range
return false;
}

if (day < 1 || day > 31) {
return false;
}

if( year < 1900 || year > 2100){
return false;
}
return true; // date is valid
}


function ValidateForm(form) {
var msg = &quot;&quot;;

if (form.program_pay_period_id.selectedIndex==&quot;&quot;){

if (form.start_dt.value == &quot;&quot;) {
msg=msg+&quot;Please enter Start Date.\n\n&quot;;
}

if (form.start_dt.value !=&quot;&quot;) {
if (!isDate(form.start_dt.value)) {
msg=(msg+&quot;You have a syntax error in the start date, please enter date in mm/dd/yyyy format. \n\n&quot;);
}
}

if (form.end_dt.value == &quot;&quot;) {
msg=msg+&quot;Please enter the End Date.\n\n&quot;;
}

if (form.end_dt.value !=&quot;&quot;) {
if (!isDate(form.end_dt.value)) {
msg=(msg+&quot;You have a syntax error in the end date,please enter date in mm/dd/yyyy format. \n\n&quot;);
}
}

if(msg!=&quot;&quot;) {
alert(msg);
form.start_dt.focus();
return false;
}

return true;
}
</script>
 
hyderabad2976,

welcome to Tek-Tips [cheers]

TGML is used here, it's a markup of sorts. Anyway when posting code as you did you see that some of it was interpreted as markup and hence the smileys.

check out the Process TGML link below the post editing TEXTAREA. It will explain how TGML works and the various methods you can employ to post code that will display correctly. Of course the simplest is to uncheck the check box.

-pete

 
Phudsen,

You make it sound like you just want us to write scripts for you. If you try building your own, you will learn something and we can be here to help you along the way.

Gary Haran
==========================
 
Hi,

The main problem is how to make the Day drop down decide how many days to list according to the month selection.

This is what I need only. I have the drop down, but all days are 31, I just want the day drop down to decide how many day.

I do not want the whole script, the form is ready and working fine, the only thing to change in the number of days listed.

Suppose I defined three array:
Array1 will have 28 days
Array2 will have 30 days
Array3 will have 31 days

Can I list the contents of the array in a drop down? If yes how?

Thanks
Paulin
 
Paulin,

Make three different drop-downs and invisibilize them in accordance with the chosen month.

So, if you pick February, the [1-30] drop-down becomes invisible, the [1-31] drop-down becomes invisible and the [1-28] drop-down becomes visible. If the display style is inline, then practically, it looks like nothing happens.

I do that funky thing here: faq216-3350

Alternately, you can verify in the onblur action of the month-chooser and the day-chooser -- it makes sure that the day is good.

Cheers,


[monkey] Edward [monkey]

Like Lovecraft? Know Photoshop? Got time for the Unspeakable?
 
Hi,

Haydarabad2976,
Thank you for you code, it is fine, but what I needed is the drop downs, the boss needs that.

Edward,
Thanks a lot, hiding and unhiding is a great idea, I will do it. It is much easier than the other way which I solved using arrays. But I am going to hide and show like you suggested, it is easier.

I already have the form and the drop downs.

Thank you both for your helpful spirit.

Gary,
I don't want you to write scripts for me. I know scripting, but I needed and idea or guidance which Edward provided. Sometimes very experienced programmers ask (How can I do that?) they look for ideas not scripts.

If someone asks on a forum, I feel that he/she is in need, so I either help him/her or no need to post cruel posts which represent immature judgement on that person.

Communication is the Key.

Thanks a lot
Paulin

 
Paulin,

I'm sorry.

My prefered method for dates is to use a calendar script. You can check out mine on my web sites
If you plan to make your own scripts with dates I recommend you upgrade the Date object with prototype.

The Date object already has plenty of cool things in it and adding some more can mean your code would be nicely organized.

Keep in mind leap years too. On leap years february is only 28 days whereas it can normally be 29. Here is a bit of code to jump start on if you wish to go that way :

Date.prototype.getDaysInMonth = function()
{
return [31,(this.isLeap() ? 29 : 28),31,30,31,30,31,31,30,31,30,31][this.getMonth()];
}

Date.prototype.isLeap = function()
{
var year = this.getFullYear();
return ( (year % 4) && (year % 100) || !(year % 400) );
}

Gary Haran
==========================
 
something like this?
Code:
<html>
<head>
<script language='javascript'><!--
function popSelect()
{
month = document.forms[0].month.selectedIndex;
monthDays = [31,28,31,30,31,30,31,31,31,31,30,31];
days = monthDays[month-1];

if (days < document.forms[0].sDay.length)
    {
    
    diff = document.forms[0].sDay.length - days;
    for (x=1;x<=diff;x++)
    {
    document.forms[0].sDay[document.forms[0].sDay.length-x].innerText = '';
    document.forms[0].fDay[document.forms[0].sDay.length-x].innerText = '';
    }
document.forms[0].sDay.length = document.forms[0].sDay.length-diff;
document.forms[0].fDay.length = document.forms[0].sDay.length-diff;
    }

document.forms[0].sDay[0] = new Option(&quot;Start Day&quot;);
document.forms[0].fDay[0] = new Option(&quot;Finish Day&quot;);
for (j=1;j<=days;j++)
    {
    document.forms[0].sDay[j] = new Option(j);
    document.forms[0].fDay[j] = new Option(j);
    }
}
//--></script>
</head>
<body>
<form>
Month : <select name='month' onChange='popSelect()'>
    <option>Select Month
    <option>January
    <option>February
    <option>March
    <option>April
    <option>May
    <option>June
    <option>July
    <option>August
    <option>September
    <option>October
    <option>November
    <option>December
    </select>
<BR><BR>
Start Day : <select name='sDay'>
        </select>
Finish Day : <select name='fDay'>
        </select>
</form>
</body>
</html>

hope it helps.

&quot;Those who dare to fail miserably can achieve greatly.&quot; - Robert F. Kennedy
 
Hi,
Thank you Gary, I did not take the Leap year into consideration. Thank you for the hints. I am sorry too for the misunderstanding. Thank you :)

Dookie2k2,
This is exactly what I needed, I just removed the fdate and added a year and made a duplicate set.

Thank you all for your help.

Best regards
Paulin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top