Posted by: c2win | November 19, 2011

How to validate start date and end date with javascript

Here is the javascript.

// enDate) {
alert(‘Start Date is greater than End Date!’);
document.getElementById(‘stDateTxt’).value = EDate;

}
else if (stDate != ”) {
var today = new Date();
if (stDate > today) {
alert(‘Select Date is greater than today!’);
document.getElementById(‘stDateTxt’).value = ”;
document.getElementById(‘stDateTxt’).focus();
}

}
}
function ValidateEndDate() {
var SDate = document.getElementById(‘stDateTxt’).value;
var EDate = document.getElementById(‘enDateTxt’).value;
var stDate = new Date(SDate);
var enDate = new Date(EDate);
if (stDate != ” && enDate != ” && enDate today) {
alert(‘Select Date is greater than today!’);
document.getElementById(‘enDateTxt’).value = ”;
document.getElementById(‘enDateTxt’).focus();
}
}
}

// ]]>

Server controls are here.

<form id=”form1″ runat=”server”>

<table>

<tr>

<td align=”right” style=”height: 28px; width: 90px;”>

<asp:Label ID=”lbDateFrom” runat=”server” Text=”Date From:”></asp:Label>

</td>

<td style=”width: 290px; height: 28px;”>

<asp:TextBox ID=”stDateTxt” runat=”server” Width=”85px” Test=”” MaxLength=”10″></asp:TextBox>

&nbsp;MM/dd/yyyy

</td>

</tr>

<tr>

<td align=”right” style=”height: 28px; width: 90px;”>

<asp:Label ID=”lbDateTo” runat=”server” Text=”Date To:”></asp:Label>

</td>

<td style=”width: 290px; height: 28px;”>

<asp:TextBox ID=”enDateTxt” runat=”server” Width=”85px” Text=”” MaxLength=”10″></asp:TextBox>

&nbsp;MM/dd/yyyy

</td>

</tr>

</table>

</form>

In code-behind,

protected void Page_Load(object sender, EventArgs e)

{

stDateTxt.Attributes.Add(“onblur”, “ValidateStartDate();”);

enDateTxt.Attributes.Add(“onblur”, “ValidateEndDate();”);

}

Javascript focus() property don’t work in Firefox. Thank!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: