Link to home
Start Free TrialLog in
Avatar of henriqmib
henriqmibFlag for South Africa

asked on

Javascript Validation for selected week days

Hi there

I need a javascript validation on change of the field to check that the date is a Monday, Wednesday or Friday. My date field is in the format of YYYY/MM/DD.

Please assist in supplying a code sample for this query.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of henriqmib

ASKER

Thanks for the help. I can't test this as I can't seem to pass this value from the textbox in the form. I am using onBlur. Any suggestions?
SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The value it is sending back is "[object]":

<HTML>
<link href="./CalendarControl/CalendarControl.css" rel="stylesheet" type="text/css">
<script src="./CalendarControl/CalendarControl.js" language="javascript"></script>
<HEAD>
<script type="text/javascript">


function testDay(dtStr)
{


    alert(dtStr)

    //dtStr = document.getElementById("dED_DeliveryDate");

    dt=dtStr.split('/');
    theDate= new Date(dt[0],dt[1],dt[2]);
    Day=theDate.getDay();
    if (Day==1 || Day==3 || Day ==5)
    {
	alert('Yes')
        return true;
    }
    else
    {
	alert('No')
        return false;
    }
}
</script>
</HEAD>





<FORM action='EventDetails.asp?screen=Insert' id=FORM1 method=post name=FORM1>
<TABLE class='results' align=center border=0 cellPadding=1 cellSpacing=1 width=700>
<TR><TD colspan=2 class='header' align=center><b>Event Details</b></TD></TR>
<TR>  <TD class='results'></TD>  <TD class='results'><INPUT id=iED_ID name=iED_ID tabIndex=1 value="" type=hidden></TD> </TR>

<TR>  <TD class='results'>Delivery Date</TD>  <TD class='results'><INPUT id=dED_DeliveryDate name=dED_DeliveryDate tabIndex=21 value="" onfocus=showCalendarControl(this); onblur='testDay(this);' style='width:80px'>  	<INPUT id=sED_DeliveryTime name=sED_DeliveryTime tabIndex=21 value="12:00" onfocus=showCalendarControl(this); style='width:40px' readonly>
	<a class=info href="#1"><B>!</B>
				<span>
					<table cellpadding=5 cellspacing=0 border=0 width=400 align=center>
						<tr>
							<td valign=top>Deliveries can only take place on Monday, Wednesday, Friday at 12pm.
							</td>
						</tr>
					</table>
				</span>
			</a>
 
  </TD> </TR>
<TR><TD colspan=2> <INPUT name=Cmd_Insert type=submit value='Continue' tabIndex=27></TD></TR>
</TABLE></FORM>

Open in new window


Apologies - Javascript is not my strength, so I can't spot the errors for why this isn't sending the value through.

You will see in my Javascript that I also tried "document.getElementById". This also didn't work.
I see my error and it's definitely linked to the onBlur: because I am using the calendar control, it doesn't pick up the value in time. I managed to get the value back when I clicked on the date for the second time.

What would be a better event to use?
While most of it works after a few changes, I still can't get the calendar control to work properly:

<HTML>
<link href="./CalendarControl/CalendarControl.css" rel="stylesheet" type="text/css">
<script src="./CalendarControl/CalendarControl.js" language="javascript"></script>
<HEAD>
<script type="text/javascript">


function testDay()
{

	dtStr = document.FORM1.dED_DeliveryDate.value





	    //dtStr = document.getElementById("dED_DeliveryDate");


	   //alert(dtStr)

	    dt=dtStr.split('/');
	    theDate= new Date(dt[0],dt[1],dt[2]);
	    Day=theDate.getDay();
	    if (Day==0 || Day==2 || Day ==4)
	    {

	        return true;
	    }
	    else
	    {

		    field2focus=dtStr;
		    alert('Please enter a Mon, Wed or Fri');
		    setTimeout(function() {if (field2focus) field2focus.focus();},40);

		//alert('Delivery can only take place on a Monday, Wednesday or Friday. Please correct the delivery date. ')
		//document.FORM1.dED_DeliveryDate.focus();
	        //return false;
	    }

}
</script>

</HEAD>



<FORM action='EventDetails.asp?screen=Insert' id=FORM1 method=post name=FORM1>
<TABLE class='results' align=center border=0 cellPadding=1 cellSpacing=1 width=700>
<TR><TD colspan=2 class='header' align=center><b>Event Details</b></TD></TR>
<TR>  <TD class='results'></TD>  <TD class='results'><INPUT id=iED_ID name=iED_ID tabIndex=1 value="" type=hidden></TD> </TR>

<TR>  <TD class='results'>Delivery Date</TD>  <TD class='results'><INPUT id=dED_DeliveryDate name=dED_DeliveryDate value="" onfocus=showCalendarControl(this); onBlur='testDay(this);' style='width:80px'>  
								<INPUT id=sED_DeliveryTime name=sED_DeliveryTime value="12:00" style='width:40px' readonly>
	<a class=info href="#1"><B>!</B>
				<span>
					<table cellpadding=5 cellspacing=0 border=0 width=400 align=center>
						<tr>
							<td valign=top>Deliveries can only take place on Monday, Wednesday, Friday at 12pm.
							</td>
						</tr>
					</table>
				</span>
			</a>
 
  </TD> </TR>
</TABLE>

<TABLE align=center border=0 cellPadding=1 cellSpacing=1 width=600>
<TR><TD align=left><INPUT name=Cmd_Insert type=submit value='Continue' tabIndex=27></TD></TR>
</TABLE></FORM>

Open in new window


And in addition to that my days seem to be 1 behind yours? ie. Saturday is 0. Why would that be?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good idea about disabling the calendar dates! I got it right and that takes the worry out of the rest. The other validation I have included on submit. Thanks for all the help!