Click here to Skip to main content
16,018,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Create a webpage with different text boxes. The webpage must provide the following functionality using JavaScript to make validation controls:

o text box to enter FROM city o text box to enter TO city, verify that the FROM and TO cities are different.

o text box to enter DEPARTURE DATE, verify that the departure date is after today

text box to enter RETURN DATE, verify that the return date is after departure date

QUESTION: How can I use JavaScript to perform the validations and show an error message if the validation fails as above?

What I have tried:

This is my code so far:

JS:
function validateCity(source, args) {
	var fromCity = document.getElementById("txtFromCity").value;
	var toCity = document.getElementById("txtToCity").value;
	
	if (fromCity == toCity) {
		 args.IsValid = false;
	} else {
		return args.IsValid = true;
	}
}


ASP.NET:
<table id="roundTrip" style="display:none;">
				<tr>
					<td>
						<asp:Label ID="Label4" runat="server" Text="From"></asp:Label>
					</td>
					<td>
						<asp:TextBox ID="txtFromCity" runat="server" Text=""></asp:TextBox>
					</td>
				</tr>
				<tr>
					<td>
						<asp:Label ID="Label5" runat="server" Text="To"></asp:Label>
					</td>
					<td>
						<asp:TextBox ID="txtToCity" runat="server" Text=""></asp:TextBox>
						<asp:CustomValidator ID="custToCity" runat="server" 
							ControlToValidate="txtToCity" 
							ErrorMessage="Not Same" 
							Display="Dynamic" 
							ClientValidationFunction="validateCity" /> 
					</td>
				</tr>
				<tr>
					<td>
						<asp:Label ID="Label6" runat="server" Text="Depart Date"></asp:Label>
					</td>
					<td>
						<asp:TextBox ID="TextBox6" runat="server" Text=""></asp:TextBox>
					</td>
				</tr>
				<tr>
					<td>
						<asp:Label ID="Label7" runat="server" Text="Return Date"></asp:Label>
					</td>
					<td>
						<asp:TextBox ID="TextBox7" runat="server" Text=""></asp:TextBox>
					</td>
				</tr>
				</table>
Posted
Updated 2-May-19 2:59am

From and To Date Validation

For validating the date use pop up calendar. So the User can easily select the date from calendar.

Steps of using popup calendar

Step 1 First Register Popup Calender on the top of of your aspx page.
<%@ Register Assembly="RJS.Web.WebControl.PopCalendar.Net.2008" Namespace="RJS.Web.WebControl"
    TagPrefix="rjs" %>


Step 2 Use Calendar with your date text boxes.
C#
 <tr>
   <td><asp:Label ID="Label6" runat="server" Text="Depart Date"</asp:Label></td>
   <td><asp:TextBox ID="TextBox6" runat="server" Text=""></asp:TextBox>
   <div style="margin-left: 130px; margin-top: -21px;">
     <rjs:PopCalendar ID="calDepartureDateFrom" runat="server" Control="TextBox6"
     From-Date="1950-01-01" Separator="/" Format="dd mm yyyy" 
     ShowErrorMessage="false" />
   </div>
</td>
 </tr>

 <tr>
  <td><asp:Label ID="Label7" runat="server" Text="Return Date"></asp:Label></td>
  <td><asp:TextBox ID="TextBox7" runat="server" Text=""></asp:TextBox>
   <div style="margin-left: 130px; margin-top: -21px;">
    <rjs:PopCalendar ID="calReturnDateTo" runat="server" 
     Control="TextBox7" From-Date="" Separator="/" Format="dd mm yyyy" 
     ShowErrorMessage="false" From-Control="TextBox6" />
    </div>
  </td>
 </tr>


Step 3 Set Departure Calendar Start Date to after Today(dissabling past dates). The Date can be set on the cs(Code behind) page on page load event.
C#
protected void Page_Load(object sender, EventArgs e)  
{
   calDepartureDateFrom.StartDate = DateTime.Now+1; //dissabling past dates
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900