1

My requirement is like so.

I need to maintain a holiday list. For that I need some validations while saving/editing an entry.

My DB contains a table with HolidayDate column. I have a textbox(jquery date picker) in the page. If the date what I choose has already been saved to the table, I should display an alert message.

Can anyone help me soon, please? Thanks in advance. Aishvarya.

MusicLovingIndianGirl
  • 5,909
  • 9
  • 34
  • 65
  • 1
    You need custom validator which can have both client and server functions to be called. Of course, for client side, Ajax needs to be used. – Sunny Apr 04 '13 at 07:57
  • you need to use server side code on text change of the texbox. Use Jquery ajax for this. [Link](http://stackoverflow.com/questions/886903/calling-asp-net-server-side-method-via-jquery) – शेखर Apr 04 '13 at 07:58
  • 1
    Maybe you can give some more details? Do you want to do this server-side or client-side? Do you want to do a postback or do this via AJAX? – Elad Lachmi Apr 04 '13 at 07:59

1 Answers1

0

Try this:

<asp:TextBox ID="DateControl" runat="server"></asp:TextBox>
<asp:RangeValidator ID ="DateValidator" runat ="server" ControlToValidate="DateControl" ErrorMessage="Invalid Date" Type="Date" MinimumValue="01/01/1900" MaximumValue="01/01/2100" Display="Dynamic"></asp:RangeValidator>

Also more information about RangeValidator here: http://msdn.microsoft.com/en-us/library/f70d09xt(v=vs.71).aspx

Sample: http://www.w3schools.com/aspnet/showasp.asp?filename=demo_rangevalidator

Simplest way of server-side validation:

code behind:

protected void Button1_Click(object sender, EventArgs e)
{
     DateTime result;
     if (DateTime.TryParse(DatePicker.Text, out result))
     {
           //handle results
     }
     else
     {
           // e.g. highlight DatePicker or handle error
     }
 }

Form:

<asp:TextBox ID="DatePicker" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

Jquery validation sample:

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>jQuery UI Datepicker - Default functionality</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css" />
  <script>
  $(function() {
    $( "#datepicker" ).datepicker({minDate: new Date(2013, 0, 01),maxDate: new Date(2013, 04, 01), dateFormat: "yyyy/mm/dd"});
  }); <!-- select only date from 1 jan 2013 to 1 may 2013 --!>
  </script>
</head>
<body>

<p>Date: <input type="text" id="datepicker" /></p>

</body>
</html>
Igor Lozovsky
  • 2,275
  • 2
  • 15
  • 14
  • My javascript function which I use only to display alert message. function Redirect(opt) { if (opt == "validate") { alert("Holiday already entered for this date...! Choose another date"); } shouldsubmit = true; window.location.assign("SetHolidays.aspx"); } – MusicLovingIndianGirl Apr 04 '13 at 08:46
  • My server side code for saving. `Dim dsGetHolDetails = dbl.usp_GetHolidaysForView().CopyToDataTable` `ViewState("HolidayDetails") = dsGetHolDetails` `Dim dtHols As New DataTable` `dtHols = CType(ViewState("HolidayDetails"), DataTable)` `For i = 0 To dtHols.Rows.Count - 1` `While dtHols.Rows(i)(2).ToString = txtHolidayDate.Text` `Dim script As String = String.Format("Redirect('{0}')", "validate")` `ScriptManager.RegisterClientScriptBlock(Me, GetType(Page), UniqueID, script, True)` – MusicLovingIndianGirl Apr 04 '13 at 08:51
  • You should add tag "VB" to your post – Igor Lozovsky Apr 04 '13 at 09:02
  • Igor, let me make it clear to you. I am NOT going to validate based on range. It is just I have some dates in the table like 01/04, 02/04, 03/04 etc., The new date or the existing one which I am going to save should NOT be equal to the ones in the database i.e, I need the code to check every single records's date column. – MusicLovingIndianGirl Apr 04 '13 at 09:28
  • I have got it, thanks all :) I just wrote a code, calling the stored procedure which fetches the list of holidays saved in the table. Like `Dim a = ` So, IF a.count > 0 I write an alert message. – MusicLovingIndianGirl Apr 04 '13 at 10:44